The skin is made up of two main layers: the epidermis and dermis. The outer layer, the epidermis, is where self-tanning occurs. The stratum corneum (horny layer) is the outermost layer of the epidermis - it is this layer that is affected by most sunless-tanning products.
Dihydroxyacetone (DHA) is the main active ingredient in self-tanning products. It does not give you a real tan with protective melanin but it can create the appearance by browning the skin.
The action of DHA is limited to the upper layers of the skin and involves a reaction between DHA, skin proteins and oxygen. The sugar in the DHA reacts with the amino acids on our skin and oxygen, turning our skin brown. The solution needs to be kept on for at least 8 hours for a full reaction. A good example is: if you take a bite out of an apple and leave it exposed to air for a few hours, it starts to turn brown. Sunless tanning works in a similar way on skin.