in a normal satr, the sun produces energy throgh nuclear FUSION, it stars by fusing hydrogen atoms to form helium (well actually by stuff like detrium but don't worry about that, hydrogen comes after that stuff), this energy and nuclear reactions forms what is known as thermodynamic pressure, this is outwards pressure so prevents the star collapsing under its own gravity.
when the star runs out of hydrogen to fuse, it momentarily can't produce any energy (not hot enough and not enough pressure to fuse heavier elements) so the star does not have any outwards pressure so it collapses in on itself, at this time, the core becomes very heavy and dense and hot, and so the outer layer expand outwards because heat rises and so on. the core is able to fuse helium. the outer layers are some what cooler. eventually the outer layers "puff" and go away from the star, probably due to lower gravity as the stars main gravity comes from and small dense core (might want to verify), this layer of gas is known as planetary nebula. whats left is the core which is called a white dwarf which when it runs out of energy is called a black/brown dwarf.
a larger star (3 times our own) goes through a different stage after red giant, neutron stars or black holes:
as it is of a greater mass, it can reach pressures and tempreatures great enough to fuse heavier elements through the tripple alpha process, eventually it starts fusing Iron (the most stable element) which instead of producing energy, it uses it, so as a core of Iron builds up…