There are various types of deposits and formations of gemstones (precious and semi-precious stones). The most important are as follows.
Magma, on cooling produce crystals of various minerals. Some of these, like diamond, are used as gemstone. Crystals formed at great depths may be brought to the surface by lava in volcanic eruptions. Minerals like moonstone, topaz and corundum are common gemstones of this type.
Towards the final stage of magmatic crystallisation, a thin silicate liquid remains and if this is squeezed into surrounding rocks, it produces pegmatites containing very large crystals. Many of these are used as gemstones. Tourmaline, topaz, beryl, quartz and spodumene are some of the common pegmatitic gem stones.
Hydrothermal vein deposits:
When most of the silicate matter in the magma has crystallized, what remains is a hot watery (hydrothermal) solution of various chemicals. These hot fluids penetrate the fissures and cracks in surrounding rocks and are precipitated in Veins. Examples are emerald, topaz, red beryl and benitoite.
Placer or Alluvial deposits:
Some minerals are highly resistant to weathering and when the rest of the rock has decomposed and disintegrated they are also carried away by flowing water. But because of their high specific gravity, they get deposited on the river bed giving rise to gem gravels such as those formed in Sri Lanka, Myanmar, India etc. Gemstones such as diamond, ruby, sapphire, chrysoberyl, zircon, garnet, quartz are commonly found in such deposits.
Chemicals carried in solution may be precipitated when the physicochemical environment changes. Common examples are malachite, opal, rhodochrosite and turquoise. E.g. when acidic copper bearing solutions move through porous volcanic rocks, they react with minerals containing aluminium and phosphorous to form turquoise.
When a rock is subjected to high temperature and / or high pressure, the chemical components of its constituent minerals get rearranged into new minerals. Gem minerals formed in this fashion are diopside, corundum, andalusite, sillimanite, kyanite, epidote, garnet, iolite etc.