“If I could offer you only one tip for the future, sunscreen would be it.” Mary Smich

Tanning is quite likely the only defensive mechanism of the human body that has achieved fashion status. Strange, because its function is not that different from that of scabs or fever: to prevent damage to the body. The change of skin colour from light to dark in reponse to sunlight is an increase in the synthesis of the dark pigment melanin. Its purpose in skin is not decorative, but to prevent DNA damage by UV radiation. In fact, UV radiation is one of the triggers that switches on the tanning response. If you’re turning brown, you have already been exposed to UV light.
UV radiation can change the DNA structure of the skin cells, which may eventually lead to skin cancers. By increasing the amount of melanin in the cells, the nuclei of the cells (where the DNA is) can be protected: melanin has a chemical structure that makes it very capable of absorbing light, including UV radiation.
The synthesis of melanin in skin cells is a complex and tightly regulated process. This is a simplified version of the entire process, which, by the way, is not fully understood yet: First, at the genetic level, proteins are upregulated: there are now more cell-surface proteins (melanocortin-1-receptors) that eventually induce melanin synthesis. These receptors activate a network of other proteins and small molecules inside the cell, which interact with each other, and with other protein pathways (for example those leading to cell death, or changing the shape of the cell to communicate with neighbouring cells) to create a balance that decides how much melanin should be synthesized. The last few steps in the synthesis pathway are no longer protein interactions, but are pure organic chemistry: small molecules are chained together by typical addition and substitution reactions to long chains of dark, black melanin.
That’s tanning.
But meanwhile, the UV radiation is also causing potentially dangerous damage to your DNA. Traditional sunscreen reflects and absorbs UV radiation to reduce the damage done to the skin cells. Some new types of sunscreen are currently in development that go further than that. One new method is to apply small fragments of DNA, much like the fragments that are created by the cell when it suffers UV damage. These small pieces of DNA are able to induce the protective mechanisms that normally only occur in reponse to damage. Another method is to add repair enzymes to sunscreen, to revert the damage already done. These technique are currently at various stages of development, and may be available within a few years.

Read more:
Skin pigmentation
UV radiation and melanoma
DNA fragment sunscreen
Enzymatic repair sunscreen