I can’t recall this being specifically dressed in canon, but we can make some reasonable guess.
First, as Umbridge says in OotP, the beings in the Forest live there under agreement with the Ministry. I think this agreement is probably motivated by fear of Muggles: Hogwarts and the Forest probably provide the best place in the UK for magical creatures to live without being found by Muggles. (Large creatures like dragons being an exception). If they wanted to run free, it’s questionable how long they’d survive in Muggle Britain without being picked off.
Being close to Hogwarts and Hogsmeade also puts them near to lots of help, if they need it. While the centaurs might take pride in eschewing human help, other species would probably benefit from people like Hagrid living nearby. One prominent example of this would be Hagrid trying to stop the unicorn killings in the first book.
We might also think that the species in the Forest are at least monitored by the Ministry and School by the fact that there’s nothing so dangerous that it would seriously challenge a Hogwarts staff member. (Here I’m thinking Dementors, dragons, Chimeras, and so on). Obviously you wouldn’t want anything like that in proximity to a school. Possibly anything that dangerous has either been wiped out in Britain, or is kept at a suitably far distance.
Finally, their containment is probably motivated by fear of the Ministry. We don’t see any instances of something that lives in the Forest using magic, so if Dumbledore or the Ministry were turn their wands on the Forest, they could probably annihilate a large part of the population. Whether they’d do that is an ethical grey area, but the mere threat of attack, or just cutting off help and protection, would probably be enough to keep them in line.
Hope some of that makes sense, and goes some way to answering your question.