Stars form from an accumulation of gas and dust, which collapses due to gravity and starts to form stars.
The process of star formation takes around a million years from the time the initial gas cloud starts to collapse until the star is created and shines like the Sun.
As gravity compresses the core of a protostar, the temperature goes higher and higher. Eventually the temperature is high enough that the star starts fusing hydrogen into helium. When the outward pressure produced by the heating of the gas by fusion energy balances gravity, a stable star is formed.