You can drive here for focal length related issues http://en.wikipedia.org/wiki/Focal_length
The most simple formula to estimate distance to the object is
where x is the size of the object on the sensor, f is focal length of the lens, X is the size of the object, and dis distance from nodal point to the object. x and f, and X and d are measured in the same units, e.g. mm and m respectively (to calculate x you'll need to estimate pixel size for your sensor; for example, for Pentax K20D it is 23.4 mm/4672 px ≈ 5.008e-3 mm/px, i.e. an image 100 px long corresponds to x = 50.08e-3 mm).
In the following I assume that the size of the object (X) is unknown, and the only known parameters are x(image size) and f (focal length).
The problem is that we cannot tell from one photo if is a small object very close to the camera or a big object far away, because the depth of field in landscape shots is usually very big (and that's why pinhole formula is applicable).
To solve this problem we may use two or more images to measure the distance. Provided you can measure all angles and distance between two camera positions, you can also calculate distance to the remote object. But measuring all angles is not an easy task.
An easier approach is to take two photos which stay on the same line with the object, with object in the center of the image. Let distance to the object on the first photo be d₁, and image size be x₁:
Then if we move the camera s meters directly towards the object, then on the second photo we have image size x₂ slightly bigger than x₁:
Which gives us
Evidently, if s is not big enough to affect image size significantly, you cannot estimate distance reliably, and need to use more complicated methods. The bigger is difference x₂ - x₁, the better.
Another formula is: -----------------------------------
distance to object (mm) = focal length (mm) * real height of the object (mm) * image height (pixels) --------------------------------------------------------------------------- object height (pixels) * sensor height (mm)