I found this interesting article describing the reactions that are involved when girls die their hair. According to the article, it is a result of a series of chemical reactions between the molecules in hair, pigments, as well as peroxide and ammonia, if present. Hair is mainly keratin, the same protein found in skin and fingernails. The natural color of hair depends on the ratio and quantities of two other proteins, eumelanin and phaeomelanin. Eumelanin is responsible for brown to black hair shades while phaeomelanin is responsible for golden blond, ginger, and red colors. The absence of either type of melanin produces white/gray hair. Some of these natural agents contain pigments (e.g., henna, black walnut shells) and others contain natural bleaching agents or cause reactions that change the color of hair (e.g., vinegar). People have been coloring their hair for thousands of years using plants and minerals. Natural pigments generally work by coating the hair shaft with color. Some natural colorants last through several shampoos, but they aren't necessarily safer or more gentle than modern formulations. It's difficult to get consistent results using natural colorants, plus some people are allergic to the ingredients.
Temporary or semi-permanent haircolors may deposit acidic dyes onto the outside of the hair shaft or may consist of small pigment molecules that can slip inside the hair shaft, using a small amount of peroxide or none at all. In some cases, a collection of several colorant molecules enter the hair to form a larger complex inside the hair shaft. Shampooing will eventually dislodge temporary hair color. These products don't contain ammonia, meaning the hair shaft isn't opened up during processing and the hair's natural color is retained once the product washes out.
Bleach is used to lighten hair. The bleach reacts with the melanin in hair, removing the color in an irreversible chemical reaction. The bleach oxidizes the melanin molecule. The melanin is still present, but the oxidized molecule is colorless. However, bleached hair tends to have a pale yellow tint. The yellow color is the natural color of keratin, the structural protein in hair. Also, bleach reacts more readily with the dark eumelanin pigment than with the phaeomelanin, so some gold or red residual color may remain after lightening. Hydrogen peroxide is one of the most common lightening agents. The peroxide is used in an alkaline solution, which opens the hair shaft to allow the peroxide to react with the melanin. The outer layer of the hair shaft, its cuticle, must be opened before permanent color can be deposited into the hair. Once the cuticle is open, the dye reacts with the inner portion of the hair, the cortex, to deposit or remove the color. Most permanent hair colors use a two-step process (usually occurring simultaneously) which first removes the original color of the hair and then deposits a new color. It's essentially the same process as lightening, except a colorant is then bonded within the hair shaft. Ammonia is the alkaline chemical that opens the cuticle and allows the hair color to penetrate the cortex of the hair. It also acts as a catalyst when the permanent hair color comes together with the peroxide. Peroxide is used as the developer or oxidizing agent. The developer removes pre-existing color. Peroxide breaks chemical bonds in hair, releasing sulfur, which accounts for the characteristic odor of haircolor. As the melanin is decolorized, a new permanent color is bonded to the hair cortex. Various types of alcohols and conditioners may also be present in hair color. The conditioners close the cuticle after coloring to seal in and protect the new color.