There was a pervasive belief in the mid 19th century that if you were poor it was because you or your ancestors did bad things. They were poor because God was punishing them.
Most of England and Europe was powered by coal. Most household furnaces were coal burning. They would take coal and put it in pans under the bed to stay warm at night. So coal had value. If you were a poor kid, you were lucky to get coal that you could use to keep yourself warm on cold winter nights. The rich had nice warm houses and lots of goodies in their stockings but the poor, who were so as punishment from God for being bad were lucky to get coal.
There are so many versions of the story, but the general idea is that St. Nicholas crept down the chimney of a poor families home to give money to a man to use as dowry for his three daughters so that they could be married. They each had stockings laid out by the fire to dry, so he left the bags of gold inside them. The stocking tradition came from this story, that if you’re a good, deserving child, St. Nicholas will put good things in your stocking. If you’re a bad, naughty child, he will simply reach down into the fire place and grab a lump of coal, as a warning for you to behave in the upcoming year.
In Italy, there is a rock candy called Carbone Dolce, it looks just like coal and parents often put this in children’s stocking as a joke. In the USA, there is a gum that comes in chunks that look like coal and is sold in tiny burlap sacks labeled “Santa’s Coal”.
Some mothers put some sort of lump of coal in the bottom of Christmas stockings so after the child receives all the toys and candy they would eventually get to the coal, to remind them that they were sometimes bad.