A barometric altimeter takes advantage of the differing density of air at varying altitudes in order to measure how high above sea level the instrument is. Out atmosphere is not an "ideal gas", but most gasses roughly approximate one in this aspect - the pressure at a given point in the atmosphere is proportional to the mass of the of air above that point. This well-understood relationship changes slightly due to meteorological factors, but is good enough for aerial use away from obstructions.
Due to altitude, geoid correction and weather system variability, some autopilots will "zero out" the altimeter at launch and measure the difference in pressure since that calibration point. Still, one cannot count on this being an accurate measure of distance above ground level if significant topography exists.