It was about four thousand years ago. Huangdi (Yellow Emperor), the first legendary ruler of China, went to the countryside to visit an old friend with his entourage. They met a boy keeping watch over a herd of horses on their way.
Huangdi asked the boy, "Do you know the way to my friend's village far away from here?"
The boy said yes.
Then the emperor asked, "Do you know my friend?"
The answer was yes again.
Huangdi thought the boy was broad-minded. So he asked him, "Do you know how to rule a country?"
The boy said, "There is little difference between ruling a country and watching over a herd of horses. You simply have to drive the wild horses out of the herd."
The emperor left, brooding over the boy's words.
The idiom is taken from what the boy said and is used to describe anyone who has a bad influence on his peers.