The spectroscopic Sloan Digital Sky Survey (SDSS) Data Release 7 (DR7) galaxy sample represents the final set of galaxies observed using the original SDSS target selection criteria. We analyse the clustering of galaxies within this sample, including both the luminous red galaxy and main samples, and also include the 2-degree Field Galaxy Redshift Survey data. In total, this sample comprises 893 319 galaxies over 9100 deg(2). Baryon acoustic oscillations (BAO) are observed in power spectra measured for different slices in redshift; this allows us to constrain the distance-redshift relation at multiple epochs. We achieve a distance measure at redshift z = 0.275, of r(s)(z(d))/D-V(0.275) = 0.1390 +/- 0.0037 (2.7 per cent accuracy), where r(s)(z(d)) is the comoving sound horizon at the baryon-drag epoch, D-V(z) equivalent to [(1 + z)(2)D(A)(2)cz/H(z)](1/3), D-A(z) is the angular diameter distance and H(z) is the Hubble parameter. We find an almost independent constraint on the ratio of distances D-V(0.35)/D-V(0.2) = 1.736 +/- 0.065, which is consistent at the 1.1 sigma level with the best-fitting Lambda cold dark matter model obtained when combining our z = 0.275 distance constraint with the Wilkinson Microwave Anisotropy Probe 5-year (WMAP5) data. The offset is similar to that found in previous analyses of the SDSS DR5 sample, but the discrepancy is now of lower significance, a change caused by a revised error analysis and a change in the methodology adopted, as well as the addition of more data. Using WMAP5 constraints on Omega(b)h(2) and Omega(c) h(2), and combining our BAO distance measurements with those from the Union supernova sample, places a tight constraint on Omega(m) = 0.286 +/- 0.018 and H-0 = 68.2 +/- 2.2 km s(-1) Mpc(-1) that is robust to allowing Omega(k) not equal 0 and omega not equal -1. This result is independent of the behaviour of dark energy at redshifts greater than those probed by the BAO and supernova measurements. Combining these data sets with the full WMAP5 likelihood constraints provides tight constraints on both Omega(k) = -0.006 +/- 0.008 and omega = -0.97 +/- 0.10 for a constant dark energy equation of state.