One of the important components that contributes to using your on-camera-flash effectively is understanding how to control its power. If you know how to do this, you can essentially turn your flash up and down to better balance its brightness with other light sources in your picture.
So here's how on the Nikon D40. (If you are in green-auto mode, forget about it. You can't control anything. Switch to P to control your flash . . . but remember, your flash isn't going to pop up unless you push the button.) Assuming you have popped up your flash you need to hold down the flash release button and the exposure compensation button while winding the command dial. I've indicated these controls in order in the following images.
As you wind, you will see a number changing in the view finder and a corresponding number changing on the LCD screen. As with exposure compensation, you will see a 0 change to things like +.3 , +.7 , +1 . . . or -.3 , .-7 , -1 . . . The negative numbers decrease the flash output and the positive numbers increase it. Remember to change it back when you are done.
Application
So what's the use? First we must consider when our on-camera-flash is useful in the first place. It's always there as a last resort as in the picture above, but its real usefulness comes when a small portion of your image needs more light to even out the exposure. Here's an example:
Elizabeth's face is too dark for the rest of the picture because she is in the shade and the background is in the sun. I can't very easily darken the background, but I can use the flash to brighten her face. So I popped up the flash and got this:
Not exactly what I want, so I employed the technique I just described and dialed the flash power down to -1. I got this:
And this at -2:
The best option would probably be between these two (maybe -1.3 or -1.7,) but hopefully you get the point. This example shows both a good example of when your flash is useful and also how to use it effectively.
_