The production of J/psi mesons is studied in pp collisions at sqrt(s)=7 TeV with the CMS experiment at the LHC. The measurement is based on a dimuon sample corresponding to an integrated luminosity of 314 inverse nanobarns. The J/psi differential cross section is determined, as a function of the J/psi transverse momentum, in three rapidity ranges. A fit to the decay length distribution is used to separate the prompt from the non-prompt (b hadron to J/psi) component. Integrated over J/psi transverse momentum from 6.5 to 30 GeV/c and over rapidity in the range |y| < 2.4, the measured cross sections, times the dimuon decay branching fraction, are 70.9 \pm 2.1 (stat.) \pm 3.0 (syst.) \pm 7.8(luminosity) nb for prompt J/psi mesons assuming unpolarized production and 26.0 \pm 1.4 (stat.) \pm 1.6 (syst.) \pm 2.9 (luminosity) nb for J/psi mesons from b-hadron decays.