Hair colour is cause by a pigment called “melanin” which is made in special cells of your skin called “melanocytes”. The melanocytes place granules of melanin in the hair as it grows giving it colour.
Melanin comes in two types: eumelanin is black or brown and phaeomelanin is yellow or red. Depending on the the type of melanin your cells make will depend on what colour you hair is.
Genes determine which type of melanin you usually make. I was lucky enough to do my PhD in the lab that discovered the gene for making red phaeomelanin. If you have a normal version of this gene you will have brown or black hair, whereas if you have a version that doesn’t work properly, you will switch to making phaeomelanin and thus have red hair.
What is odd, is that blonde hair is actually caused by eumelanin (the brown or black pigment). It seems that the difference is to do with how the pigment is structured, and thus how the light bounces off it. We don’t really know how that works yet and what gene(s) are involved in making the difference.
Hair colour is determined by 2 pigments- eumelanins and pheomelanins. Different combinations of these pigments produce the range of hair colours that people have. Pheomelanins produce the red colour and eumelanins result in black or brown colours. A person who produces very little eumelanin will have blonde hair. HIgh concentration of eumelanins will result in black or brown hair (so I have lots of eumelanins!). People with red hair produce lots of pheomelanins. Older people have grey or white hair because their bodies don’t produce so much of the pigments anymore, thus resulting in lighter coloured hair.
The combination of these pigments is the result of the genetic makeup of a person. I think Darren has give a good explanation for this part. Go take a look at some of his answers to similar questions!
And then some people will bleach or dye their hair. Bleaching hair destroys the melanin pigments, usually destroying the eumelanine first which is why some people go ginger when they bleach their hair. Some hair dyes work by forming dye molecules within the hair strand – that’s why they are a funny colour when you apply them but then it comes out OK on the hair, usually reds and browns and blacks. Other hair dyes are direct dyes where the dye just sticks to the hair strand. Those are usually bright colours like pink, purple and green.