Believe it or not, hair does not turn gray. It is believed that all hair is white to begin with . What you see as hair changing from a color to "gray" is actually the death of pigment cells within the hair shaft.
Melanin is what causes hair to have color. Melanin is produced before birth, and the amount of melanin, along with how it is distributed through hair is what dictates which color hair the baby would have. If melanin was never produced, the hair would remain white.
Melanin is made of special cells called melanocytes. These melanocytes are produced through the hair shafts. As the hair grows from the shaft, the melanocytes inject pigments, or melanin, through the shafts that attach to the keratin-producing cells, or keratinocytes.  These cells contain the main proteins that make up the hair. As time goes on, more pigment is produced, which creates the various natural hair colors.
There are actually two types of melanin that occurs in the hair: phaeomelanin and eumelanin. Phaeomelanin is responsible for the light colors (red and yellow) in hair, and eumelanin is responsible for the dark colors (brown and black) in hair. They two types of melanin are mixed in different amounts to produce different shades in hair color.
There is no definite answer as to why hair eventually becomes gray. Many factors ranging from age and stress to hormones and genetics, etc. can affect individuals differently and at different times during their lives. However, it is believed that there is eventually a point when a person's melanocytes start slowing or completely stopping production of pigment through the hair shaft.
If you notice a color change in the hair you already have, that means that the process of pigment production has started to slow down and that these hairs are ready to shed and fall out. When new gray hair appears, it means that there are still some healthy melanocytes producing pigments in new hair growth, and when new white hair appears, it means that they have all died and completely stopped producing pigments in all future hair growth.