The short answer
To get everything in focus, use a narrow aperture (a large f-number such as f/8 to f/16 on many full-frame cameras), focus about a third of the way into the scene, and use a wider lens if you can stand close to the foreground. For the most depth, focus at the hyperfocal distance. If one frame cannot cover the whole scene, take several frames and combine them.
Stop down, but not too far
A narrower aperture gives deeper depth of field. Moving from f/5.6 to f/11 usually gives a noticeably deeper zone of sharpness, which is often enough for a landscape.
Going much narrower does not always help. Very small apertures bring diffraction, which softens the whole image. On many full-frame cameras it becomes visible somewhere around f/11 to f/16. On smaller sensors, the point comes sooner. Test your lens if you can, and look at the corners and the distant details at full size.
Narrow apertures also reduce the light reaching the sensor. You may need a slower shutter speed, a higher ISO, or a tripod to keep the exposure sharp.
Focus about a third of the way into the scene
The zone of sharpness extends farther behind the focus point than in front of it. For a scene with a near foreground and a distant horizon, a useful starting point is to focus about one-third of the way from the nearest object to the farthest. This pushes the far end of the zone as far out as possible while keeping the foreground acceptably sharp.
This is a rule of thumb. It works best when the scene has a clear nearest object. Check the foreground on your screen at full size, because the near edge is the first place softness shows up.
Use hyperfocal focusing when you need the most depth
The hyperfocal distance is the focus distance that gives the greatest depth of field at a given focal length and aperture. When you focus at it, the zone of sharpness runs from about half that distance to infinity.
Many photographers use it as a simple setting. Work out the distance for your focal length and aperture, focus on that point, and check that your nearest object is beyond the near limit. A depth-of-field scale on a manual lens or a calculator can help. The full explanation and the formula are in the article on hyperfocal distance.
Choose a wider lens and stand closer to the foreground
A wider lens gives more depth of field at the same aperture when the framing is the same. A wide-angle lens also lets you include a foreground close to the camera while keeping the background in view. You can often get a sharp near object and a sharp distant one without stopping down far.
Be careful with very close foregrounds. An object only a few centimeters from the lens can be too close to hold sharp at all apertures, even with a wide lens. Step back a little if the nearest object looks soft.
Keep the foreground in mind
It is easy to focus on the horizon and forget the near edge of the frame. Before you take the shot, look at the bottom corners of the frame and the nearest rock, flower or leaf. If those are soft, move your focus closer, use a narrower aperture, or change your framing so the nearest object is farther away.
When one frame is not enough
Some scenes need more depth than any reasonable aperture can give, especially close-ups and macro work. In these cases, focus stacking is the usual answer. Shoot a series of frames with the focus moved slightly between each, then blend them into one image with software. Each frame contributes the part that is sharp in it, and the result keeps a moderate aperture without the softness of heavy stopping down.
Focus stacking needs a steady camera and a scene that stays still. Moving water, leaves in wind and people walking through the frame will cause problems. For a landscape with still elements, it can be a good way to keep both the foreground and the distance sharp.
Quick checklist
- Choose an aperture around f/8 to f/11, and test whether f/16 looks softer on your lens.
- Focus about one-third into the scene, then check the nearest object on screen.
- Use a wider lens if the foreground is close.
- For the most depth, try hyperfocal focusing.
- If the scene still cannot be sharp everywhere, take several frames and stack them.