An ultrasonic sensor measures distance by emitting high-frequency sound waves and then measuring the time it takes for the waves to bounce back after hitting an object. This information is used to calculate the distance between the sensor and the object.