Stars are formed from massive clouds of dust and gas in space.
Gravity pulls the dust and gas together to form a protostar.
As the gases come together, they get hot. A star forms when it is hot enough for nuclear reactions to start. This releases energy, and keeps the star hot.
During the main sequence period of its life cycle, a star is stable because the forces in it are balanced. The outward pressure from the expanding hot gases is balanced by the force of the star’s gravity. Our Sun is halfway through its 10 billion year stable phase.
Gravity pulls smaller amounts of dust and gas together, which form planets in orbit around the star.
This slideshow shows the life cycle of a star: