WARNING: Long reply incoming
To start with, you might find these guides to the D40 useful (found on Google) -
http://www.digital-slr-guide.com/nikon-d40-guide.html and
http://www.kenrockwell.com/nikon/d40/users-guide/index.htm.
Secondly, you are right. It is all to do with the aperture. Ignoring ISO for the moment (we'll come back to it), there are two things that have an effect on your picture ... the aperture and the shutter speed:
As your aperture gets larger (going from f22 up to any one of f5.6, f4 or f2.8) you let more light into the picture. To stop the picture being overexposed (too bright) a faster shutter speed is needed. Similarly as your aperture decreases in size (tends towards f11 or f22) then you need a slow shutter speed to let lots of light in and stop the picture becoming underexposed (too dark). Taking a picture with the correct exposure is simply a case of balancing the two.
Here's where it gets tricky though. Although you can balance these in any way you like to get the correct exposure, changing the aperture changes the depth of field. The smaller the aperture, the greater the depth of field. This means that at f22 (small aperture) you'll get almost everything in focus, whereas at f2.8 (large aperture) very little in the frame will be in focus beyond the main subject of the image, i.e. there will be a small depth of field.
Your camera would seem to be trying to give you a fast shutter speed (which is normally a good thing) by increasing the size of the aperture ... but as we just discussed, this means your depth of field will be narrower.
So, what can you do about it?
Well, Canon cameras have a setting called (iirc) A-Dep which tries to automatically give you a large depth of field. The Nikon D40 does not appear to have that. There are point-and-shoot "program modes", so you could try picking one of those. Generally if you're shooting landscapes, turn the dial to the mountain image. This will tend to give you a large depth of field. If you're shooting portraits, turn to the appropriate icon for shots that will tend to give small depths of field.
Also, it's possible you could try playing around with the ISO. What the ISO does is to determine the sensitivity of the sensor. The higher the ISO, the more sensitive the sensor is to light. If it's at 100 (the lowest) then it will behave "normally" and give the best quality, however if you increase the sensitivity to 200 or 400 then you will be able to use a small aperture (i.e. greater depth of field) with the same shutter speed. On modern cameras like your one, you may find you can increase the sensitivity to as much as 800 without really seeing any noticeable decrease in quality.
Having said that, I believe the D40 will be set to auto-ISO so it might not have a huge effect!
If that doesn't answer the question, you could try scouring the manual and see if that has any further tips for you ...
http://www.nikonusa.com/pdf/manuals/noprint/D40_noprint.pdf.
Good luck ... you've done the right thing in getting a D40, and it may seem like a hell of a steep learning curve, but it will be worth it. The pictures will seem a world apart from your compact's ones!
