The force of gravity is continually trying to compress the Sun towards its centre. If there were not some other force counteracting gravity, the Sun would collapse. Outward pressure is produced by the radiation from nuclear energy generation in the Sun's interior. NGC 3603, a giant HII region in the Milky Way. Ultraviolet light from the young star cluster in the centre of the nebula is heating and ionising the surrounding hydrogen gas.
Stars form from concentrations in huge interstellar gas clouds.
These contract due to their own gravitational pull. As the cloud gets smaller it loses some of the energy stored in it as gravitational potential energy. This is turned into heat which, in the early days of the embryonic star, can easily escape and so the gas cloud stays cool. As the cloud's density rises, it becomes more and more difficult for heat to escape and the temperature at the centre of the cloud rises. If the cloud is big enough, the temperature rise is sufficient for nuclear fusion reactions to begin.