These days, you use a GPS. You have satellites that know exactly where they are in space, and those satellites are constantly “shouting” their exact position and time. Your GPS receiver can then find several (usually three) satellites at a time, and see how long it took for signal from all of them to reach the receiver, and do some math to find exact latitude, longitude, and elevation.
In the early days, before satellites, this was done by surveying. First, you would measure the horizontal position from some reference point, and them measure the angle up from that reference point. From there, you can make a triangle and know the height. This requires that you be able to see your target from your reference point. For things close to the ocean, that reference point would usually be the ocean, but further inland, you would often have long chains of reference points – measure one, use it to find the next, so on.
If you go hiking, at least in the US, you’ll sometimes see little metal disks in the tops of hills and mountains that look something like [this](https://en.wikipedia.org/wiki/Survey_marker#/media/File:DarienCTUSCoastAndGeodeticSurvey1965Marker11042007.jpg) in the ground. These are survey markers, meaning these are points where the position and elevation have been precisely measured, and so can be used to find the position and elevation of other things in the area. They’re usually at the tops of hills, because most old surveying methods needed to be able to see the point being measured from the reference point.
The strength of gravitational pull of the earth varies with height (more specifically distance from the center of the earth). It is maximum at sea level and decreases slightly both upwards and downwards. So an object would weigh more at a beach than on a mountain or under ground.
So if you knew what an object weighed at the beach and at the point of measurement, you can use the difference in weights and some simple math to get the difference in elevation between the beach and the the city.
Note : The decrease in gravitational pull is very slight and not noticeable. It requires precise instruments to measure but the logic is similar.
I believe it is also possible to estimate elevation based on other factors like air pressure which is also maximum at sea level and decreases as you go higher and higher.
Latest Answers