Villagers are fickle creatures. If the conditions aren’t perfect, then you will end up with a deserted village.
When it comes to leaving villages, this usually happens as a result of poor access or low food supplies.
However, villages may be deserted if the villagers are killed by mobs. Though this isn’t technically an example of villagers leaving, it has the same effect.
To retain your villagers, the first thing you need to do is make sure that the village is well lit and protected.
A well-lit village reduces the chances of mob spawns, which makes it safer for the villagers. You should also aim to leave the village before nightfall to keep mobs away.
In terms of keeping the village safe, you can try building walls and defenses to keep mobs outside. You do need to make sure that your villagers can move through these defenses freely. This means making doors and gates.