Stars begin to form from clouds of gas in space. The cold temperatures and high densities (compared to elsewhere in space, but would be considered a vacuum on Earth) of these clouds allow gravity to overcome thermal pressure and start the gravitational collapse that will form a star.
A protostar looks like a star but its core is not yet hot enough for fusion to take place. The luminosity comes exclusively from the heating of the protostar as it contracts. Protostars are usually surrounded by dust, which blocks the light that they emit, so they are difficult to observe in the visible spectrum.
An artists rendition of the birth of a star. Credit: NASA/JPL-Caltech/R. Hurt
Sometimes the formation of stars can be encouraged or sped up by disturbances in the gas clouds that compress the gas such as other nearby stars or supernovae.
As the cloud collapses, it begins to spin and by the time a protostar is formed, the cloud flattens and there is a protostellar disk spinning around the protostar. These disks probably slow the rotation of the protostar, and sometimes coalesce into planetary systems.
As the protostar rotates, it generates a strong magnetic field. The magnetic field also generates a strong protostellar wind, which is an outward flow of particles into space. Many protostars also send out high-speed streams or jets of gas into space. Usually there are two jets flowing out along the rotation axis of the protostar. Eventually the wind and the jets clear away the extra gas around the protostar and allow the protostar to come into view.
A protostar becomes a main sequence star when its core temperature exceeds 10 million K. This is the temperature needed for hydrogen fusion to operate efficiently.
The length of time all of this takes depends on the mass of the star. The more massive the star, the faster everything happens. Collapse into a star like our Sun takes about 50 million years. The collapse of a very high mass protostar might take only a million years. Smaller stars can take more than a hundred million years to form.
Star formation by collapse of molecular clouds
This animation is a simulation of the collapse and fragmentation of a molecular cloud presented in "The Formation of Stars and Brown Dwarfs and the Truncation of Protoplanetary Discs in a Star Cluster" by Matthew R. Bate, Ian A. Bonnell, and Volker Bromm (http://www.ukaff.ac.uk/starcluster/). The calculation models the collapse and fragmentation of a molecular cloud with a mass 50 times that of our Sun. The cloud is initially 1.2 light-years (9.5 million million kilometres) in diameter, with a temperature of 10 Kelvin (-263 degrees Celsius).
In a newly formed star cluster, there are many more stars with low masses than stars with high masses. For every star with a mass between 10 and 100 solar masses, there are typically 10 stars with masses between 2 and 10 solar masses, 50 stars with masses between 0.5 and 2 solar masses, and a few hundred stars with less than 0.5 solar masses. As time passes the balance shifts even more toward smaller stars because the higher mass ones die first.
Stars above about 200 solar masses generate power so furiously that gravity cannot contain their internal pressure. These stars blow themselves apart and do not exist for long if at all.
A protostar with less than 0.08 solar masses never reaches the 10 million K temperature needed for efficient hydrogen fusion. These result in “failed stars” called brown dwarfs which radiate mainly in the infrared and look deep red in color. They are very dim and difficult to detect, but there might be many of them, and in fact they might outnumber other stars in the universe.