The article in Outdoor Photographer
that Chas mentions is a good place to start for learning about lenses. Give it a good reading and then if you have more specific questions, feel free to ask.
As for lenses for the D40 you need to be careful in what you pick because in order to get full functionality out of the lens, I.e.: autofocus, metering, etc. the lens itself has to contain the focusing motor. For Nikon series lenses you would need to obtain AF-S and AF-I type lenses. See here
for more info. Check out the Compatible Lens section on that page. Other brand lenses will code their lenses differently but basically you will be looking for lenses that have internal focusing motors in them.
As for cost, lens prices cover an extremely wide range. You can find some for just a few hundred dollars (or less) up to several thousands of dollars. In my internet wanderings I have found a particular Canon lens with about 8 gazillion mm range for about $125,000. (ordered it yesterday!)
Seriously, though, the most important investment in a dSLR you can make is in the lens (glass). The better your glass, the better results you will get out of your camera. And a lens you buy today will service you long after you upgrade from the D40 to the D3X I know your just dying to have! I know budgetary considerations come in to play here, but you need to think about where you want to go with your photography and balance that against what you can afford and choose the best glass you can get with the money you have to spend.