It is to do with the amount of melanin produced by our skin cells! Melanin is produced by cells called melanocytes in a process called melanogenesis. Melanin is triggered by an enzyme called tyrosinase, which creates the color of skin, eyes, and hair shades. The amount and type of melanin produced is controlled by a number of genes which we inherit from our mum and dad. One copy of each of the various genes is inherited from each parent. Each gene can come in several alleles (versions), resulting in the great variety of human skin tones.
Melanin controls the amount of ultraviolet (UV) radiation from the sun that penetrates the skin by absorption. While UV radiation can assist in the production of vitamin D, excessive exposure to UV can damage health.
It is generally true that people from hotter countries have darker skin and this may be because in the past these people survived better because they didn’t get so damaged by the sun as people with fair skin! 🙂