Villagers usually tell you when they're thinking about moving (they make a pinging noise and get those alarmed symbols over their heads when they see you) and you get the option to tell them to stay. If you're unsure about which dialogue means they're telling you about moving, I believe there's lists of the things they say. They'll usually tell you that they want to travel the world or go to another town.
You can also hear about it by talking to other villagers. If you talk to them enough, sometimes they'll tell you that another villager is thinking about moving. In that case, you can go find that villager so that they can ask you the question. If they aren't asking but you know they're moving, you can talk to them a few times, save your game, reload, and try to find them again. In one case, I did that a few times and the villager still wasn't asking me, so I set my 3DS clock a month forward, started as a new character, quit without saving, and set the date back to load the game as my mayor again. Then I repeated the process of finding them, talking to them, saving and quitting, reloading, and finding them again until they did. Using a new character to time travel into the future sped up the process.
Villagers won't ping you about moving if there are any events coming up, though, so sometimes you have to be careful and patient.
Once a villager has made up their mind to move (either automatically or because you told them to go), if you talk to them a few times, they'll usually tell you something about how they're thinking about their decision and you can ask them to stay. Sometimes it's also possible to do this when a villager is in boxes, but not always.
If your villager is in boxes and you want to keep them but they're not asking you about it, you can find someone to hold your villager for you by inviting them into their town. After that, once you move out a total of 16 villagers, you can invite your old villager (or an identical version of them, if you couldn't find someone to hold them) back.