Drilling mud provides wellbore stability by exerting hydrostatic pressure to balance formation pressures, preventing well collapse or blowouts. It also forms a filter cake on the wellbore walls, which helps prevent fluid invasion and maintains wellbore integrity.