This article reports on the inclusive production cross section of several quarkonium states, textrm{J}/psi , psi mathrm{(2S)}, Upsilon mathrm (1S), Upsilon mathrm{(2S)}, and Upsilon mathrm{(3S)}, measured with the ALICE detector at the LHC, in pp collisions at sqrt{s} = 5.02 TeV. The analysis is performed in the dimuon decay channel at forward rapidity (2.5< y < 4). The integrated cross sections and transverse-momentum (p_{textrm{T}}) and rapidity (y) differential cross sections for textrm{J}/psi , psi mathrm{(2S)}, Upsilon mathrm (1S), and the psi mathrm{(2S)}-to-textrm{J}/psi cross section ratios are presented. The integrated cross sections, assuming unpolarized quarkonia, are: sigma _{textrm{J}/psi } (p_{textrm{T}} <20 GeV/c) = 5.88 ± 0.03 ± 0.34 ~mu b, sigma _{psi mathrm{(2S)}} (p_{textrm{T}} <12 GeV/c) = 0.87 ± 0.06 ± 0.10~mu b, sigma _{Upsilon mathrm (1S)} (p_{textrm{T}} <15 GeV/c) = 45.5 ± 3.9 ± 3.5 nb, sigma _{Upsilon mathrm{(2S)}} (p_{textrm{T}} <15 GeV/c) = 22.4 ± 3.2 ± 2.7 nb, and sigma _{Upsilon mathrm{(3S)}} (p_{textrm{T}} <15 GeV/c) = 4.9 ± 2.2 ± 1.0 nb, where the first (second) uncertainty is the statistical (systematic) one. For the first time, the cross sections of the three Upsilon states, as well as the psi mathrm{(2S)} one as a function of p_{textrm{T}} and y, are measured at sqrt{s} = 5.02 TeV at forward rapidity. These measurements also significantly extend the textrm{J}/psi p_{textrm{T}} reach and supersede previously published results. A comparison with ALICE measurements in pp collisions at sqrt{s} = 2.76, 7, 8, and 13 TeV is presented and the energy dependence of quarkonium production cross sections is discussed. Finally, the results are compared with the predictions from several production models.