All about DSLR lenses?

So I'm buying a dslr camera, and I'm wondering how the lenses work? Are all lenses like interchangeable? I'm getting the Nikon D3100. What lenses are best quality for best price, and/or recommended?
Can somebody just tell me how dslr camera lenses work? Can you get like any lens you want, or does it have to be specific to the exact model you're buying?

You must get a lens that has a mount compatible for your brand or model of camera.
Example. Nikon/nikkor lenses will work on nikon cameras.
Canon lenses will work on canon cameras
Other lens manufacturers like sigma, tamron and so on will list what brand/mount it is available for.
If you are getting a beginner DSLR camera like the D3100 then you must make sure the nikon/nikkor lenses you buy are listed as AFS, or other lens brands describe them as having a focus motor. This is because beginner DSLR's don't usually have a built in focus motor and if you don't use a lens that has one built in then you will have to use manual focus.
Most lenses being made today have them built into the lenses anyway, so its not always a big deal at all.

If you buy a Nikon dSLR, you can only use Nikon-F mount lenses on it. If you buy a Nikon D3100, and you want to use autofocus all the time you are limited to AF-S and AF-I lenses only.
http://www.kenrockwell.com/...s.htm#dslr
With Canon, there's no such limitations outright. You will just be shocked to learn that your old lenses won't be recognized by your new camera later when you upgrade. Beware of Err99.
With Sony, you have no problem with compatibility. You will have a problem of choices however as there are few available.
Pentax has also a very limited number of digital lenses but it makes up for it by making all lenses it has ever made since the company began and all other branded lenses that were made for their cameras to work with all current dSLR models.

At lenses you don't just look at prices. You first look at their range, because you one buy to shoot something specifically. Either macro, wildlife, landscape, everything etc.
A Nikon lens should work with all Nikon models. However, your D3100 doesn't have an AF motor so not all lenses will focus with it (some older ones). But you still have like 30+ lenses to choose from, here's a post that will explain ii all to you.
Here's a Lens Buying Guide - http://www.the-dslr-photographer.com/2009/11/buying-a-lens/