By damaging your skin.
People need sunlight to produce Vitamin D. As people moved from Africa into northern countries that did not get enough light to produce adequate Vitamin D, they evolved to produce less of the protective melanin pigment that blocks ultraviolet radiation and the damage it can cause.
Instead of producing melanin pigment in their skin all the time, these people only produce a little, unless the skin detects damage from ultraviolet light. Then it starts producing more melanin. It takes about three days for the melanin to be produced, so if there is a lot of sun exposure before the tan is produced, you will get a sunburn.
You get a little bit of tanning effect right away, because the longer wavelength UVA light causes damage to what melanin exists, causing it to combine with oxygen. This makes it darker.
But most of the tan is from increased production of melanin, which takes longer.
The color of the skin without melanin can be seen inside the mouth. The pink color is due to the color of the blood flowing in the fine capillaries, and the scattering of light by the skin cells, which would look white if it were not for the red blood. The red and the white combine to look pink.
Ultraviolet light damages skin in several ways. It breaks up DNA, which is why it can cause cancer. It is the products of DNA destruction that trigger the tanning process, so you can’t get a tan without first damaging your skin and risking cancer. But the sun also damages the proteins that make the skin flexible. This results in dry and wrinkled skin, and the effect we call premature aging, since it makes people who are in the sun a lot look much older than people who get less sun.
Since we can now get Vitamin D from milk and supplements, and we have effective sunscreens (and clothing and hats), there is little reason to risk cancer by excessive sun exposure.