Crystals form in rock that is the earth. They arrive there dissolved in super heated water and gases. The inner core of the planet is so hot that stone becomes liquid and moves as oceans far below us. Because the crust is forever floating and moving, cracks are continuously created and molten magma is eager to invade. Under tremendous pressures and heat, the earth's rich cocktail of chemical compounds, steam and a multitude of gases come rushing upward to fill in the gaps.
Vapors fill a space then cool.
Deposits of minerals crystallize. Solids formed this way are called igneous rock. Minerals may join with other minerals to form specific stone types. This as called electrostatic attraction. Quartz, feldspar and mica are good examples.