Origin of the idiom keep at bay?

This is not really an idiom. Baying is a continuous barking by hounds. To keep something at bay means to prevent its escape by surrounding it with barking dogs, or by extension, to prevent a problem from getting out of control by maintaining constant vigilance.