KEEP AT BAY and HOLD AT BAY are two renditions of an idiom that has been in use since the mid-1700s.
"The government hopes to keep inflation at bay."
If you keep something or someone unpleasant at bay, you appear to be in control of the situation, maintain a distance, prevent them from approaching, harming, attacking, or affecting you.
"Vitamin C helps to keep colds at bay."
If you move to a new city you can keep loneliness at bay by joining a club, playing a group sport or taking a class. A good way to keep your feelings of anxiety at bay is to meditate or do some sort of relaxation technique.
"The armed robber held police at bay for about 9 hours."
You can keep illness at bay by eating healthy food and getting enough sleep and exercise. And you can hold your desire for chocolate at bay by not buying it and keeping it in your house.
"Some wolves prowled round the camp, but were kept at bay by the fire."
The Old French word 'abbay' means 'barking'. These came into English, first as 'abay' and later 'at bay'. Hounds that were barking were said to be 'at a bay'. When dogs were kept at bay, they were kept from attacking the prey.
"40 police were still needed to keep at bay crowds gathered outside the house."
When a hunted animal is at bay, it is trapped by the hounds and forced to turn and face them. If the animal is successfully defending itself, it is holding the hounds at bay.
"Two green glazed lions guarded the gates to hold evil spirits at bay."
To keep at bay meant then to be in a standoff with a baying dog that was intent on killing - a scenario which also gave us the expression 'baying for blood' - to demand punishment or retribution.
"Sandbags kept the floodwaters at bay."
Later, the phrase 'keep at bay' takes on the more general meaning of 'fend off'. The earliest example which doesn't refer directly to hunting with dogs is from The Derby Mercury, 1759, in a report of England's war with France:
"We have seen the French kept at bay for the whole campaign, and they are gone into their winter quarters."