Grey hair is one of many age related traits. Other traits showing a positive correlation with age include Parkinson's, cancer, and Alzheimer's. There are two key theories as to why age-related disease & decline occurs. But the key message is just because something evolves, it doesn't mean it's advantageous.
First of all is mutation accumulation (MA) theory. This theory basically suggests that as we age there is less likelihood that selection can remove the alleles that cause genetically determined aging. If we consider something like cancer, which is clearly deleterious it becomes clearer, though grey hair may be deleterious (reduces fitness) it is at least less obvious. If a cancer has early onset, affects people aged 10, then it is highly unlikely to spread through a population because those carrying the allele are unlikely to reproduce. If a cancer has late onset, causing cancer at 60 years old, then there is a much lesser chance it will affect reproductive success - most 60 year olds don't have more children in the future. In the case of MA the effect of selection is weak if a trait reduces survival post-reproduction. The same could be true of grey hair, even if it is costly it's arrival late in life is not that surprising.
The other theory is antagonistic pleiotropy (AP) which suggests that genes which improve fitness might reduce survival. For example, the genes that cause cancer at age 60 might also make you better at attracting mates, fertilising, and raising offspring. In this case it would be beneficial to carry the cancer gene (from an evolutionary perspective). I think there is some evidence (but can't find it right now) that grey hair is associated with higher fitness, in that case it could be that grey hair has evolved directly because of sexual selection (people might prefer silver haired partners) or because of AP - grey haired people are better at other fitness related things because of pleiotropy.
So to answer your question: direct selection is not necessarily the reason that grey hair has evolved, it could be because of mutation accumulation or pleiotropic gains associated with the genes affecting grey hair.
You can read more about these theories of aging in this answer and for references.