  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 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. 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. 
