Betamethasone works by binding to glucocorticoid receptors, leading to changes in gene expression that promote the production of surfactant in the fetal lungs. Surfactant is crucial for lung function as it reduces surface tension, preventing the collapse of alveoli during exhalation.