A star forms from massive clouds of dust and gas in space, a nebula, which are mostly composed of hydrogen. Gravity pulls the dust and gas together. As the mass falls together it gets hot. A star is formed when it is hot enough for the hydrogen nuclei to fuse together to make helium, which releases energy, keeping the core of the star hot. During this stable phase in the life of a star, the force of gravity balanced by higher pressure from high temperatures. When all the hydrogen are used up in the fusion process, larger nuclei form and the star expands to become a red giant. When all the nuclear reactions are over, a small star like the Sun begins to contract under the pull of gravity, becoming a white dwarf. A larger star with more mass will go on making nuclear reactions, getting hotter and expanding until it explodes as a supernova, throwing hot gas into space. This could become a neutron star or a black hole.