A CPAP machine works by providing a constant stream of air through a mask worn over the nose or mouth, preventing the collapse of the airway that causes sleep apnea.