They measure air pressure. As you ascend in the atmosphere, the pressure drops in a fairly predictable manner, so you can pretty accurately measure how high you went. This needs clean air and a port to equalize pressure, and clean subsonic airflow. This sometimes is inaccurate during boost, but it is almost always fine once at apogee and during descent.
GPS is used by some altimeters; that uses triangulation grin satellites. As long as the radio reception isn't hindered by metal or carbon fiber, or high speeds or acceleration, you get fairly accurate readings independent of altitude.