For altitude callouts during the landing phase (2500, 1000, 500, 400, ... 100, 50, 20, 10), the computer simply reads the data from the radar altimeter.
The radar altimeter uses radio waves to measure the vertical distance between the airplane and any ground / obstacle below. The radio altimeter is mounted on the underside of the fuselage. They are usually accurate up to feet. This is different than the standard altimeter in the cockpit, which measure air pressure.
The radio altimeter has no idea where the runway is; if there is uneven terrain leading to the airport, or if the pilots' descend profile does not match the glideslope, the readings (and thus the callouts) will not reflect the height above runway elevation.
Note that the airlines can customize the altitude callouts. Different manufacturers also uses slightly different callout patterns.
The second type of audio alerts are messages from a Terrain Awareness and Warning System (TAWS). These systems uses radio altimeter, and may combine information from other sources, such as GPS and a global terrain database, to issue alert messages to the pilots if terrain impact is imminent.
Since the radio altimeter is only capable of detecting ground directly below, it is unable to provide timely warnings in certain situations, e.g. approaching a steep hill. By integrating with GPS data and a terrain database, the computers can predict the flight path of the aircraft and issue warnings if a risk of collision exists.