Animal Crossing friend codes are special codes that let two to four players meet up in the same town over Wi-Fi. Their main purpose is to let you visit a specific person's town directly.
The reason they exist is that you can't just search for random towns and drop into them; instead, you exchange friend codes with someone you know, register each other, and then you're able to travel to their town. It's a simple system that keeps visits to people you've actually connected with.