There are different types of baldness, that happen for different reasons, but the most common type is called “male pattern baldness” (also called “androgenic alopecia”). Men usually get this type of baldness when they get older.
The major cause is a hormone called dihydrotestosterone (DHT). Male pattern baldness is caused by an increased sensitivity of hair follicles to DHT. It causes the hair follicles to shrink, which shortens their lifespan. Eventually the follicles die which means no more hair will grow. We don’t really know why some men are sensititve to DHT and some are not, but it seems to be largely genetic.