A cloud of dust and gas forms a nebula. The force of gravity pulls the layers of dust and gas together to form a protostar. When the temperature is high enough, molecules of hydrogen gas undergo nuclear fusion to form helium. This keeps the core of the star very hot. This continues happening and the star is now known as a main sequence star. Here, the force of thermal expansion is equal to the force of gravity meaning the star is stable. When the hydrogen eventually runs out, the force of gravity becomes greater than the force from thermal expansion so the star is compressed. Eventually, the star's outer layer expands outwards, as it does so, it cools down leaving behind a red giant or red supergiant depending on the size of the star.
1. If the star is small to medium sized like the sun:
The outer layer of dust and gas gets lost into space, leaving behind a dense, hot solid core known as a white dwarf.
2. If the star is big:
It undergoes nuclear fusion with heavier elements available so the force of thermal expansion increases. This causes the star to bright brightly again. Eventually the star becomes unstable and explodes in a supernova by ejecting its outer layer of dust and gas into the universe. this leaves behind an extremely dense solid core known as a neutron star or black hole (massive stars).
Hope this helped :)