Going outside and soaking in some sunlight is good for you. The sun helps your skin develop an important nutrient know as vitamin D. This nutrient keeps your bones strong. However, if you stay in the sun too long without protection, bad things can happen. You can get a sunburn, or worse, you can develop various types of skin cancers.
Thus, humans need sunlight but they also need protection from it. To deal with this, our species has evolved a variety of skin colors. The evolution of these traits was caused by the intensity of sunlight humans experienced in different parts of the world.
Near the equator, the sun shines intensely throughout the entire year. Here, humans who evolved darker skin colors were better able to protect themselves from the harmful effects of this intense sunlight. On the other hand, near the north and south poles, the sunlight is less intense. Here, humans who evolved lighter skin colors were better able to absorb enough sunlight to produce vitamin D and stay healthy. Overall, this has resulted in populations of people having darker skin near the equator and lighter skin near the poles.
Skin color is a complex trait. Your DNA controls some aspects of which skin color you develop. But like with every trait, the environment also has some impact. For example, the amount of sunlight you get each day controls whether your skin stays the same color, gets more tan, or develops a sunburn. The sun makes these changes by acting directly on your cells and by regulating the expression of your DNA.