How Sunlight Works
Sunlight arrives on earth in three forms: infrared (heat), visible light and ultraviolet. Ultraviolet light is classified into three categories:
One of the interesting things about UV radiation is that it is reflected by different surfaces. These reflections can amplify the effects of UV exposure. For example, snow reflects 90% of UV light. That is why you can get snow blindness and severe sunburns from skiing on a sunny day. Sand can reflect up to 20% of UVB that hits it, meaning that you can get extra UV exposure at the beach.
On the other hand, certain things absorb almost all UV radiation partially or completely. Glass is one of these substances -- many glasses are very good absorbers of UV (which is why you may have heard that you cannot get sunburn in a greenhouse -- just make sure it is glass and not plastic covering the greenhouse!). Most sunscreens use chemicals that have the same UV-absorbing properties.
So, now that we know all about the skin we can start to actually understand tans and sunburns. When you get a tan, what is actually happening is that the melanocytes are producing melanin pigment in reaction to ultraviolet light in sunlight. Ultraviolet light stimulates melanin production. The pigment has the effect of absorbing the UV radiation in sunlight, so it protects the cells from UV damage. Melanin production takes a fair amount of time -- that is why most people cannot get a tan in one day. You have to expose yourself to UV light for a short period of time to activate the melanocytes. They produce melanin over the course of hours. By repeating this process over 5 to 7 days pigment builds up in your cells to a level that is protective.
The previous paragraph applies to Caucasians. In a variety of other races, melanin production is continuous, so the skin is always pigmented to some degree. In these races the incidence of skin cancer is much lower because cells are constantly protected from UV radiation by melanin.
Melanocytes actually produce two different pigments: eumelanin (brown) and phaeomelanin (yellow and red). Red heads happen to produce more phaeomelanin and less eumelanin, which is why they don't tan very well. In albinos, the chemical pathway that produces melanin cannot proceed because an enzyme called Tyrosinase is missing. Therefore albinos have no melanin in their skin, hair or irises.
Melanocyte-stimulating hormone (MSH) is produced by the pituitary gland. MSH flows through the bloodstream and reaches the melanocytes, encouraging them to produce more melanin (for example, a person injected with a large dose of MSH will get darker). The pituitary gland is actually quite interesting -- it is tied into the optic nerve, which means that it can sense light. If you have ever raised chickens for eggs, you know that in the winter egg production falls way off. You solve this problem by providing light in the chicken coop. The extra light stimulates the pituitary gland in chickens, which causes the gland to produce a hormone essential to egg laying. In humans, light affects the pituitary gland as well and one result is the production of MSH. A funny side-effect of all of this is that wearing sunglasses may make you more susceptible to sunburn! See this page for some thoughts on the subject.