I think another thing that strengthens John 's statments but is often forgotten in an explanation is simply that the longer barrel exposes the bullet to the pressure curve longer, giving it time to build up more velocity. Those gases will continue to expand and increase velocity of the bullet until extending barrel length is no longer practical. Dan Lilja has experimented with barrels in 46"-50" range producing extremely high velocity, but not very practical...
So the question is --How long do you want your barrel to be? OR How much velocity do you want? Barrel weight increases exponentially with its length to keep the same stiffness, so most of our factory barrels are the best of nothing--only a comprimise of several factors to make them manageable on hunting weight rifles...
For example:
Benchrest barrels are typically heavy contour, but short, aiding in extreme accuracy, but giving up a little velocity.

Factory sporter barrels are thinner and lighter, giving up accuracy and stiffness, but picking up a little velocity in the process.

Long range hunting rifles and Varmint rifles are usually very heavy, as they use stiff, long, thick(heavy contour) barrels and are after the most accuracy and highest velocity, mostly to help with with bucking the wind.

The differences between 20 inches, 22 inches and even sometimes 24 inches, will probably never be felt by whatever you hit with that bullet- <img src="/ubbthreads/images/graemlins/smirk.gif" alt="" />-You might notice that the rifle is much easier to carry in the field, though.
I have a couple of guns with 28-32" barrels, though, and I'll take them anyday in a velocity shootout......