Here's a quote from a book published in 1610: "Drinking, eating, feasting and revelling till the cows come home as the saying is." If it was 'a saying' 400 years ago, there's little hope that we'll ever discover where it originated. It's simply proverbial, in other words.
I should have added to the answer above that cows are creatures of habit...they know when it's milking-time, when it's feeding-time etc. Similarly, they know when it's time to head back from the field to their shed for the night. So, "until the cows come home" just means until the very end/last moment.