The bane of micro and macro photographers! The more magnification, the less depth of field.
Depth of field or DoF, can be explained as the distance between the furthest and nearest areas of a specimen that remain in focus.
For closeup photography, you may be able to alter the size of your camera's aperture, sometimes known as F-numbers or f-stops (see Wikipedia article). Basically, the smaller your aperture (a higher F-number), the more depth of field you get. However, the quality of your image can deteriorate.
It pays to experiment. Try bracketing your shots, that is take a number of shots with changed settings. Remember that you no longer have to pay for film developing!
I have found through experience that I generally get a better quality image with my “film speed”, my ISO setting at 100 rather than set to automatic. This generally means longer exposure times, but this is not a problem when using a tripod.
Photographing minerals through the microscope is much tougher. Remember, the more magnification, the less depth of field. Flat specimens are ok, but looking down into a cavity can be very hard to get everything you want in focus. This is where Stacking Software comes into its own.