Stars form from clouds of gas and collapse under self-gravity.
The collapse is stopped by internal pressure in the core of the star. During the collapse, the potential energy of infalling hydrogen atoms is converted to kinetic energy, heating the core. As the temperature goes up, the pressure goes up to stop the collapse. Since most stars are over 10 billion years old, then they must be producing its own energy rather than shining on leftover energy from formation. They are stable in size.