In general, this is done through photogrammetry: analyzing the positions of objects in the image and comparing them to the positions of those objects in other images. If you have a collection of surface images (eg. the 123 surface photos taken during Apollo 11), you can calculate the relative position and orientation of the camera where each photo was taken; if you can correlate features in the surface images with ones from an orbital image, you can get an absolute position.
With good images, you can get highly accurate relative positions this way: in 1969, the Apollo 11 photos were located to within 1.5 meters; a modern re-calculation produced uncertainties typically less than half a meter.
Other methods can be used (eg. the Mars rovers know how far they've driven and in what directions), but photogrammetry is still used to get the initial position and to correct for accumulated errors.