Beware that when we talk about apertures high numbers (16 or 22) indicate small openings and low numbers (2,8 or 4) mean large openings.
The smaller the aperture, the larger depth of field. However, you should know that DOF extends 1/3 in front of the point of focus and 2/3 beyond it. So by focusing at infinity, you are in fact wasting 2/3 of your depth of field. Focusing at a point in front of infinity and choosing a small aperture will maximize DOF. This is called hyperfocal focusing.
If your camera has a depth of field scale, here is how to do it. Focus your camera at infinity, note the distance on the camera's lens opposite the chosen aperture and set your lens at this hyperfocal distance. Now everything from foreground to infinity will be sharp.
It is best not to choose the widest or smallest aperture setting because lenses do not show their optimum image quality at both extremes of the scale and optical faults may become visible. For optimum image quality select a medium aperture.
In low light situations, fully automatic cameras give preference to fast shutter speeds and select the widest aperture setting for a correct exposure, so depth of field will be minimal.
If there is no manual way to select a slower shutter speed keep your subject away from the borders of your image as this is where optical faults are most prominent.