Melanin is produced by melanocytes found in the basal layers of the epidermis. Manufacturing begins when these melanocytes (following the instructions of DNA) construct enzymes and the amino acid Tyrosine. Production happens in little sacs called melanosomes. Inside these sacs - the enzymes catalyse a long chain of chemical reactions (the oxidation of tyrosine in turn catalyses the conversion of L-DOPA into Dopaquinone) to convert tyrosine into two types of melanin: eumelanin (which is brown or black) or pheomelanin (orange or yellow).
Once the melanosomes are packed with melanin - the melancytes ship them off to keratinocyte (which are barrier cells in the superficial layers of the skin) where they become more visible. Inside the keratinocyte the melanosome responds to sun exposure by convering the nucleus of the cell (like an umbrella) - protecting the DNA.
The number of melanosomes and the mix of pigments determines sun protection as well as skin colour.