It's a way to get extra missions in Mystery Dungeon, you can replay missions you've written down the code for, play the same missions as your friends via the codes and get official event missions from the Pokemon Company.
Additionally, most people use online code generators to get certain rewards that they want.