When a star exhausts its hydrogen fuel, it leaves the main sequence and enters the red giant phase. In this phase, the core contracts and heats up, while the outer layers expand and cool. For low to intermediate-mass stars, this phase culminates in the shedding of outer layers, forming a planetary nebula, with the core remaining as a white dwarf.