This is an edit --- I got the math wrong the first time – forgive an 80 ¼ year old for having senior moments. Gentle readers, please feel free to re-check and edit. By the way, this ratio method works!
Suppose the camera you own is categorized as a compact digital. You look up its specifications and find the image sensor measures 16mm height by 24mm length. Further, it is a 16-megapixel sensor. You can calculate the ratio of height to length 24 ÷ 16 = 1.5. Now you can calculate the horizontal and vertical pixel count and find it to be 1,033 vertical and 1,549 horizontal (algebra solves). Further, you calculate the pixel size, and find them to be a square with sides that measure 0.0155mm.
Now you photograph an object and examine the image height; it spans 750 pixels. Thus the image height is 750 X 0.0155 = 11.6mm
When you took the picture you were using a 200mm focal length lens. Now a lens projects an image of the outside world onto the surface of the image sensor. If you know the focal length of the lens, you can use this value as the distance, lens to sensor.
We can draw an imaginary trace of the image forming rays. We trace from the top and from the bottom of the image to the center of the lens. We thus draw a triangle. The height of this triangle is the focal length = 200mm. The base of this triangle is 11.6mm. This is the image triangle.
Now an object triangle can also be traced. The base of this triangle is the actual height of the object. The height of this triangle is the actual distance, object to lens. The angles of the image triangle and the object triangle are identical.
You know the height of the image triangle is 200mm (focal length). The image spans 11.825mm -- thus the ratio is 200 ÷ 11.6 = 17.2
Now if the object being imaged is 100 meters away from the camera. You can calculate the height of this object thus -- 100 ÷ 17.2 = 5.8 meters.