Star formation is the process by which a star forms from a gas cloud. It is believed to occur in two stages: first, the contraction of a dense molecular cloud core into a protostar; and second, the accretion of material onto the protostar to form a main-sequence star. The initial mass of the molecular cloud core is believed to be an important factor in determining the final mass of the star.
The vast majority of stars are thought to form in clusters within giant molecular clouds (GMCs). GMCs are massive (up to several million solar masses), cold (10-20 K) and extended (up to hundreds of light-years across) reservoirs of dust and gas. Within GMCs, smaller clumps of dense gas collapse under their own gravity to form individual stars or small stellar groups.
As a GMC collapses, its density increases and it begins to heat up due to compression. At some point, the temperature and pressure become high enough for hydrogen atoms to fuse together, igniting nuclear fusion and forming a protostar. The accretion of additional material onto the protostar continues until it reaches its final mass and luminosity.
The time scale for star formation can vary widely depending on the mass of the initial molecular cloud core. For example, low-mass cores (<0.1 M_sun) can collapse and form stars in as little as 10^5 years, while more massive cores (>100 M_sun) can take up to 10^7 years or longer. Stars also continue to accrete material throughout their lives, although at a much slower rate than during formation. For example, our Sun is thought to have accreted about 1% of its current mass over its 4.6 billion year lifetime.