In the gallery are three images of a fly (by Muhammad Mahdi Karim - own work, CC BY-SA 3.0). The first is a single image, focused close, and the second is focused far. The third is a "stacked" set of six images, taken at different focal points and combined in software to create an image with a deep depth of field that shows the entire fly in focus.
Some modern (and generally expensive) cameras have a feature called "focus bracketing", where they can automatically take a series of images at different focal points over a specified range. A few of these cameras can create the stacked (combined) image in-camera, but many require that the stacking be done by a program on an external device (computer or smartphone app)—and the external method is generally better in either case. This in-camera focus bracketing has some limitations, and is best used with images no more magnified than about 1x ("life size"). For higher magnifications, especially when using microscope lenses (which may reach 50x or more), it is far better to keep the camera's focus fixed and moved the subject relative to the camera to select the focal point of each individual image that will be stacked.
Under perfect conditions (lots of light for short exposures, and a steady hand), it may be possible to take adequate source images with a handheld camera, especially if only a few are needed (low magnification). But it's better to mount the camera (or subject—there are advantages to each way) on a rail with a moveable sled that can be precisely positioned, and this is absolutely essential at higher magnifications where the movement distance may be on the order of millimeters all the way down to micrometers (millionths of a meter, commonly called "microns"). These devices are called "focus rails".
A focus rail may be either manual (with the positioning done by turning a knob, and the camera shutter triggered by hand for each shot) or powered (where the sled is positioned using a stepper motor, and the camera is automatically triggered by the same device that powers the stepper—called the "controller"). A reasonably-good manual focus rail might cost around US$100, whereas a powered one will cost at least a few hundred, and in some cases thousands, of dollars. Generally, the automatic one will have its software and features locked down—something that everyone here loathes, of course. But with some effort—a little or a lot, depending on how ambitious you are about the cost and the results—anyone can build their own automatic focus rail.
Before I go any farther about the hardware, a note on the software. When one stacks (combines) multiple images into one, it is absolutely essential that the source images be precisely aligned with each other and that details like exposure be consistent. Otherwise, the result will just be a blurry mess that is even worse than the worst source image. If you're feeling crazy you could try adjusting & combining the images manually using pretty much any image editing software, but if there are more than two or three source images that is going to get old very quickly. But Photoshop (for example) has a built-in stacking function, and you may find that's all that you need. However, in more complex situations (especially if you're combining hundreds of source images, which is likely the case with macro photography at higher magnifications), you may be happier using a dedicated, more sophisticated program like Helicon Focus or Zerene Stacker (I'll leave the discussion about which is better to someone else).
One important consideration for the hardware is whether you're going to mount the camera, or the subject, on the moveable sled, while keeping the other in a fixed position. For a large subject, especially one that you can't move (say, something in a natural setting), your only option is to put the camera on the sled. But for small, portable objects you have a choice, and, in general, I recommend moving the subject rather than the camera. Vibration...Read more »