That's because your hair grows out from the roots, and as you age the pigments (melanin) that would normally keep your hair a certain colour are not produced anymore becuase the pigment producing cells (melanocytes) start to die.
They're likely differnt colours in the middle becauase that will be the result of a gradual dying off of the melanocytes at the base of that strand. As a result the root (newest part of the hair) will be gray when they have mostly all died off, the middle will have a bit of melanin because it was produced when there were still some functional melanocytes, and the tip (the oldest part of the hair) will still be full of melanin from when it was produced when the melanocytes were alive.
As for why this happens it's not fully understood why the melanocytes die as we age, but the gene Bcl-2 has been found to be essential for the maintenance of melanocytes. Mutations in this gene could be responsible for premature graying . There is some literature on the role of Bcl2 family genes as regulators of cell death  but how these things are connected seems largely still unknown.
There have been some reports of people indicating that a change in lifestyle and/or diet have led to a decrease in gray hairs, or even that the hair begins to regain its colour leaving a gray section at the tip or in the middle. I've only found one relevant article which makes reference to some research into the restoration of hair growth and colour in mice. An American group are studying the role of the Wnt signaling protein in controlling the activity of melanocytes. I'm just speculating here, but it could be that fluctuations in Wnt protein production (possibly influenced by diet and lifestyle and overall health) may switch melanocyte melanin production on and off. I haven't been able to find any literature to back up this hypothesis though.