Interstellar molecular clouds of dust and gas exist in a quasi-stable balance between self-gravity and internal pressure. If the mass and density are large enough, and the temperature and pressure low enough, they can collapse to form stars, usually in clusters of a few dozen to a few thousand. Both gaseous and magnetic pressure can be factors.
Various factors can trigger collapse, including shocks from collisions with other interstellar clouds, passing through one of the spiral density waves that give spiral galaxies their names, or the shockwave of a nearby supernova. Once a collapse starts, it can become irreversible, as gravity gets stronger as the cloud shrinks. The cloud will heat up as a result of the collapse, but if it contains plenty of dust, it can radiate that heat away in the infrared.
Actual stellar formation seems to take place in localized regions of overdensity called cloud cores. Because the molecular clouds are turbulent, individual pieces are likely to have a good deal of angular momentum, and pieces of the cloud with significant specific angular momentum will instead orbit about the center. Since the angular momentum is randomly distributed, many of these orbits will intersect and the high angular momentum parts will collapse into a disk of residual net angular momentum.
The collapse into the disk will convert substantial gravitational potential energy into heat, as the resulting shockwaves heat at least the inner disk to over 1500 K. This will evaporate nearly all the dust grains in (at least) the inner disk. Farther from the central star, the converted potential energy is less, and some interstellar grains may be unconverted and even remain quite cool.
The elemental composition of the disk is expected to be like that of the central star (and the parent molecular cloud), mostly hydrogen and helium with a sprinkling of heavier elements. It seems, though, that at least for the inner planets, both hydrogen and helium and other volatile compounds (H2O, NH3, etc) are swept out before they can be bound to a planet by gravity.