Stars in the main-sequence stage give out energy as light and heat. This energy is released by nuclear fusion reactions deep in their cores. The reactions fuse hydrogen nuclei to create helium. Stars spend about 90% of their lives in this stage. Our Sun is about 5,000 million years into its 10,000 million year main-sequence.
Stars contain a lot of mass which has a lot of gravity. The gravity pulls inwards, which could cause a star to collapse in on itself. In a main sequence star, this inward pull is balanced by the outward gas pressure due to the nuclear fusion reactions deep in the star's core. This balance is called hydrostatic equilibrium and stops main sequence stars from collapsing.
If the amount of energy released by a star gets smaller, then the forces are no longer in balance. The force of gravity wins and the star begins to shrink and contract. This contraction raises the temperature and pressure deep within the star which lets the core release more energy. The outward gas pressure gets higher until it balances the inward pull of gravity and the star stops collapsing.
The mass of a star controls how long it stays in the main sequence stage. More massive stars use up their fuel more rapidly than less massive ones. When a star runs out of fuel, it can no longer keep the forces in balance. At this point, a star expands and cools to become either a red giant or a red supergiant star.