Folks, ya’ll are focusing on skin cancer much too much. Skin cancer tends to kill you after prime reproduction age, meaning natural selection doesn’t do much to eliminate it. Sure, having grandparents does give a kid a slight advantage, but other adults in an extended family can provide similar benefit.
These days, I’m leaning towards the folate and vitamin D angle. A woman must have adequate folate levels to successfully reproduce. Lack of them greatly increases the risk of neural tube defects that, prior to the 20th century, killed the kid a birth or shortly thereafter - and that’s natural selection in full strength. Darker skins protect folate by reducing damage from the sun. And if you take a closer look at skin color and latitude you’ll find equitorial people who live in forested areas or those with frequent cloud cover have a tendency to be a somewhat lighter shade of dark brown than those living in, say, Sudan which is not only equitorial but arid, so there is seldom cloud cover and not a canopy of vegetation overhead. Likewise, people in high latitudes (that’s Inuit, as an example) have less atmosphere above them to filter out damaging sun, and during the summer the sun shines up to 24 hours a day - so for a couple months their sun exposure may be quite intense. And contrary to rumor, it does warm up in the summertime, sometimes to 80 or 90 degrees due to all that constant sunlight, which will prompt anyone to peel off that fur parka for a bit. So there may be some pressure to keep skin pigment. This may also account somewhat for the Tasmanians and Australians keeping their dark skins - those folks didn’t wear much clothing and thus got a lot of sun exposure, particularly in the arid regions of their territories. Likewise, frequent ocean voyages in primative boats with little shade might have exerted some pressure on Polynesians to keep their brown.
On the flip side, a dark skin interferes with vitamin D production - and that is a factor pushing for lighter skin. A pregnant woman needs adequate vitamin D not only for herself but also for that developing baby. If the child’s skeleton doesn’t get a good start in the womb there will be consequnces later on. In the sun-intense regions, you can produce sufficient vitamin D even with a very dark skin - but it’s almost universal for the women of a given ethnic group to be slightly lighter than the men. Possibly because they need the vitamin D. The less sun you get - such as by going north - the more pressure for lighter skin to allow for sufficient vitamin D. And since those less-sun-intense regions also result in less damage to folate, that lighter skin isn’t as much a drawback as it might be somewhere else.
A diet heavy in vitamin D food sources might take off the pressure towards lighter skin, resulting in people who retain dark pigmentation because it does no harm. This may be happening with the Inuit - but with the Inuit of today you also have to take into account that at least one of the early non-Inuit artic explorers was African-American and left his genes behind. Some of his descendants are aware of the relationship, some may not be, but there are a number of relatively dark, somewhat curly haired Inuit who are more representative of modern mixing than some “pure” ancestral state. People move around and they screw each other, which keeps us all one species.
So, really, this can get quite complicated - we’ve already uncovered cancer, folate, and vitamin D as factors in the natural selection of skin color, and we need to add in cultural factors for sexual/cultural selection on top of that. No wonder we come in so many shades!