All three are interelated in how the picture turns out
The following things will make more light come into the camera or be reistered on the photos: higher ISO , larger aperature or longer shutter speed.
These decrease the light: Lower ISO, smaller aperature, or shorter shutter speeds
ISO is how sensitive the sensor or film is to light, the higher the number the more sensitive ( higher iso = more grain/noise )
large aperatures are those with smaller numbers so an f4 lets in twice as much light as an f8 ( large aperature also gives shallow depth of field )
the longer the shutter is open the more light that reaches the film or sensor 1/30th second lets light in for twice as long as 1/60 so that is twice as much light ( movement blurrs on the image at slow shutter speeds faster shutter speeds freeze motion )
If I were getting a proper exposure at f4 and 1/125 second I could also get a good exposure at f8 and 1/60th of a second. I doubled the light by increasing the shutter speed but cut it in half by making the aperature smaller so in truth the same amount of light came in.
ISO can be set on auto or you can choose it. Generally the lowest that works is best but at times you need to bump it up to shoot in lower light.
You can set the camera in program where it will choose the shutter speed and aperature. Flexible programs let you adjust it
You can set it for aperature priority where you pick the aperature and based on the meter readings the camera sets the shutter speed. This can easily cause your blurring if the camera keeps the shutter open longer to compensate for the aperature.
you can set it on shutter priority where you pick the shutter speed and the camera sets the aperature within the range of the lens.
you can set manual where you pick both
The D40 also has several automatic modes like portrait, night etc.
One thing to think of that may be part of your problem the flash built in on the D40 is only good to about 12 feet. If you are letting the camera choose the flash settings and the subject if further from you than that you will not get good pictures
I hope this helpsorry for the typos ~lol~