Science fiction author Arthur C Clarke famously said "Any sufficiently advanced technology is indistinguishable from magic." Usually when you discover how a magic trick works, it stops being magic; it becomes much less impressive. But when you discover how GPS works, it becomes even more impressive. So how does it work?
The Satellites
The first step is to put a bunch of satellites into a geostationary orbit. Which means: the satellite orbits above the equator, moving at the same speed as the surface of the earth. One complete rotation every 24 hours. This means that wherever you are on the surface of the earth, the satellite appears to be in the same spot in the sky.The magic in doing this is to match the satellite speed and the height of the orbit. To stay in a lower orbit a satellite has to move faster; a higher orbit needs a slower speed. Satellites have to be right over the equator 35,786 km high and travel at a speed of 3.07 km/second to be geostationary.
Satellites need to be far enough apart not to interfere with each others' radio signals and not to bump into each other. So there's only room for a limited number of satellites in the geostationary orbit. The International Telecommunication Union assigns slots for geostationary orbit and settles disputes between countries about slots. It seems magical that there's enough cooperation in the world to do that!
It's hard enough to put a satellite into space at exactly the right height and speed. But the difficulty doesn't end there. Satellites drift out of position due to solar wind and the gravitational pull from the sun, moon and planets. (Low orbit satellites are also affected by atmospheric drag, which is negligible for high-orbit geostationary guys). So the satellite operators have to monitor the position and use tiny rocket engines on the satellite to correct the position periodically. These corrections are known as stationkeeping.
So Where Are We?
To determine its location the GPS receiver measures its distance from at least three, but usually four different satellites. Doing that needs more advanced technology magic. Each satellite broadcasts data on radio waves. Some of the data it sends is about its exact location. Some of it is a unique sequence of digits together with the exact time it started sending the sequence. The GPS receiver receives the signal after a delay of about one tenth of a second - the time it takes for light to travel from the satellite to the receiver. Knowing this time of travel (T) and the speed of light (S = 300,000 km/second) makes it easy to calculate the distance (D = S x T).But there's a catch to measuring that fraction of a second's lag in the signal. If the clock in the satellite isn't perfectly synchronized with the clock in the receiver, the distance calculations will be wrong. An error of one nanosecond (one billionth of a second) will make the calculated distance wrong by 30 cm (1 foot). Satellites are equipped with $50,000 atomic clocks that are accurate to 2 nanoseconds (2 billionths of a second) each year. But that's unaffordable for a receiver like a cell phone. The solution is a very clever trick. The receiver gets a cheap quartz clock that's accurate to only 100 nanoseconds a day. The trick is to get frequent clock corrections as a by-product of calculating its location. How?
Let's look at the process in two dimensions. If the clocks on the satellites and the receiver are all correct, the distances (d1, d2 and d3) will be correct and the receiver can calculate its position as the single point where the three circles intersect.
If the receiving clock is running slow the time lag will be measured as longer than it really is. The calculated distance will be too long, and the three circles will overlap, instead of intersecting at a point.
And if the receiving clock is running fast, the circles won't touch at all.
So the magic trick is to shrink or expand the measured circles by the same proportion until they intersect at a point, which is where the receiver is. Then the receiver calculates how much its clock must be running fast or slow, and corrects the clock. Just like magic.
In real-world 3D, distance from a satellite provides a sphere, not a circle. So the calculations are more complicated, but the principle is the same.