According to Roots Web:
This apparently refers to an old English (Welsh?) belief that keeping a goat in the barn would have a calming effect on the cows, hence producing more milk. When one wanted to antagonize/terrorize one's enemy, you would abscond with their goat rendering their milk cows less- to non-productive.
Click here for another side to the story - though C is of course right. It suggests a gambling angle to the 'stolen goat' idea.
I'm not sure where the 'on' came from, however, though we certainly do say that in British usage, as there is no suggestion af actually mounting the wretched creature!
According to Albert Jack in his book 'Red Herrings an White Elephants' :
"This is an American phrase from the early part of the 20th century. Goats have long been considered to have a calming influence on horses and for this reason were often the stable mates of highly strung race horses, especially while being stabled at an unfamiliar race track. But sometimes a fancied stallion's chance of success would be torpedoed by shady opponents, who would slip into the stable and take the goat away. The result was an irritated and nervy racehorse who performed badly in the race."