In my page about time-lapse photography, I described the free software called CHDK (the Canon Hack Development Kit) that I can load directly into my camera, that takes over its firmware and adds a lot of very nice features.
One of the nice features is focus bracketing. This allows you to take a series of shots with one button press, and have each shot focus a little bit farther away each time.
While this can be nice by itself to ensure that one of the photos has the focus you really wanted when focusing can be difficult, where it really shines is when you use software that combines all of the photos into one shot where everything is in focus.
This is called focus stacking, and there are several free programs that do this for you. The one I use is CombineZP.
In the camera you can control how many millimeters the focus changes for each shot, and in which direction (forward or back, or both). And if that isn't enough control, all of the features in the camera can be controlled in the camera by user-written BASIC programs, to give you all the flexibility you want.
The photo below shows the first and last of a series of shots at different focus points.
Notice that in the left photo, the closer parts of the flower are in focus, but the stem is not. In the photo on the right, the stem is in focus, but the front of the flower is blurred. Also, the flower looks larger in the photo on the right, since the focus has shifted closer. The software takes this into account when it combines the pictures into one.
The photo below is the result of stacking those in CombineZP:
You can see that the entire flower is in focus, from the stamens in front to the end of the stem in the back. Even the wood grain in the table is always in focus.
Here are some more examples:
I generally use from 6 to 20 shots for each take. CombineZP has several different methods of combining the photos, and for each series, some work better than others. So I run them all (there is a single-click menu item for that) and select the one that works the best.