This little delight caused some controversy in the 1980s, when it was banned in many UK schools for reasons of
political correctness. Apparently any use of the word "black" was enough to get the PC brigade's knickers in a twist.
The rhyme itself probably dates back to the 18th century. Its origins, as with many rhymes of the time, are obscure. Some say it was written as a protest against the "Old Custom" export tax imposed in Britain in 1275, the "master" being King Edward VI. It was a complaint against the hardships caused to the poor by the enclosures of land for sheep farming.
There are probably as many stories explaining the origin as there are singers of the song. Such is the wonderful world of folklore. Another explanation frequently given is that superstition stated that black sheep were in some way unlucky or cursed, and consequently gave less wool.)
Some also make a connection to the slave trade, although it's notable that many of the people who put this theory forward are the same people who argue for the {Bowlderisation] of the rhyme for reasons of political correctness.
Myself, I favour the simplest answer, that it's a kid's nursery rhyme, about sheep.