Hydrothermal veins are a combination of the two ways that crystals form. Magma contains water as well as molten rock. Because it is underground, and under tremendous pressure, the water stays a liquid. At very high temperatures and pressure, that water can dissolve quite a few minerals. As the magma cools, the last part that is still a liquid is the quartz and the high temperature/high pressure water. They flow into cracks in the surrounding rocks, where they cool. The quartz starts to solidify quickly, but the hot water keeps some of it dissolved.