We study the sub-Riemannian structure determined by a left-invariant distribution of rank n on a step-2 simply-connected nilpotent Lie group G of dimension n(n+1)/2. We describe a transitive group action that leaves invariant the sub-Riemannian structure. By using geometric optimal control theory techniques, we derive necessary conditions for the length-minimality of the sub-Riemannian geodesics. We perform an integration process for the associated Hamiltonian system that yields explicit expressions for the extremal curves and the corresponding sub-Riemannian geodesics, the obtained formulas allow the complete parametrization of the exponential mapping in terms of algebraic invariants of the problem.