Everything to do with elevation and GPS is a matter of black magic, and making the best of imperfect data.
Altitude can be worked out from GPS signals alone, but it's a bit hit and miss, as GPS is really all about giving you lat/long.
Altitude can be worked out from a barometer - but that needs to be calibrated, and can drift.
Altitude can also be derived from survey data, if you know where you are and have access to some kind of reference. This reference could be a map, or it could be the the elevations along a planned route.
Garmins (and probably other manufacturers, but I can only speak from experience with Garmins) will "auto-calibrate" the barometric altimeter by using GPS elevation and a black magic calibration algorithm.
Some Garmins can also force-calibrate the altimeter if you set a waypoint with a known elevation, when you ride past that waypoint it will calibrate to that elevation. I have several of these set on my usual ride-starting routes. Other Garmins allow you to manually calibrate.
When you download a route as a tcx file it contains elevation data of the upcoming route derived from whatever mapping software was used to plan the route. So my Edge 530 can see upcoming climbs because it knows the upcoming elevation changes along the route. But if there is no route loaded it doesn't have a clue - it doesn't use mapping data, only route data.
Some Garmins have a feature called "climb pro" that surveys your route when you load it and figures out where the hills are (it normally does a very good job of this, but can be fooled by particularly nutty routes). Then it alerts you when you start a hill and tells you how far you have to go and gradient remaining - all derived from the route elevations. This is normally pretty good, but sometimes it can beep you and tell you that you have just completed a climb while you are still struggling up to the top of a 15% gradient.
The GPS can figure out its own instantaneous gradient from altimeter and GPS data. If it has a route loaded it can also figure out a predicted gradient from route elevation. Hopefully these don't disagree too much and the Garmin "climb pro" uses a black magic algorithm to fudge this problem.
As
@ColinJ says the displayed gradient lags behind reality a bit. I don't find it too bad, about 15 seconds or so.
There may be some particularly clever devices that use mapping data (not route data) to predict upcoming hills. I don't know. I don't have one.
Of course, the displayed gradient on your screen is never going to agree with the gradient that Strava or RideWithGPS tells you when you review your ride afterwards. And as we know from many other threads lots of different devices and methods and black magic algorithms will come up with different values for total climb.
Sometimes I think that rather than moaning about discrepancies and inaccuracies, we should applaud the lengths and black magic that the likes of Garmin, Wahoo, RideWithGPS, Strava and so on have gone to in order to give us some reasonably consistent data.