Vitamin D is not a single chemical but rather a group of related compounds: ergocalciferol (vitamin D2), a 1:1 mixture of ergocalciferol and lumisterol (vitamin D1), cholecalciferol (vitamin D3), 22-dihydroergocalciferol (vitamin D4) and sitocalciferol (vitamin D5).
When these compounds reach the liver they are converted to the prohormone calcidiol and then to calcitriol by both the kidneys and by macrophages in the immune system. Calcitriol is a hormone that regulates the concentration of calcium and phosphates in the blood and this is why vitamin D is essential for the growth and remodelling of bone and why a vitamin D deficiency leads to rickets.
A molecule of cholecalciferol.
A vitamin is a substance which the body requires but which it cannot produce by itself. Vitamin D does not fall into this category because the body can produce its own Vitamin D when exposed to sunlight: the major forms of vitamin D, the D2 and D3 forms, are produced in the skin when precursor chemicals (provitamins) react with ultraviolet light.
Vitamin D3 is formed when 7-dehydrocholesterol, itself formed by the oxidation of cholesterol from dietary fats*, reacts with medium wave ultraviolet (UVB) light with wavelengths between 270 and 300 nanometres. Vitamin D2 is formed in a similar reaction when dietary ergosterol reacts with UVB light to form viosterol and then D2. Fungi are particularly high in ergosterol, making them an important source of provitamin D for vegans.
* This is debated, see for example Hartmut Glossmann, “Origin of 7-Dehydrocholesterol (Provitamin D) in the Skin”, Journal of Investigative Dermatology 130 (2010): 2139–2141. doi:10.1038/jid.2010.118.