Another analogy: friend codes are like phone numbers: you can connect directly to others using them, but you have to communicate them to one another by other means. In order for a friend code to be valid, both people have to enter each other's friend code (you can't just enter a friend's code without them entering yours), you only have to enter them once, then they are added to your list. For example: In Metroid Prime: Hunters, you can play with "Find a game," which pairs you with (ideally) 3 other players who are looking for a game (I say ideally, because there are not always 3), there is no communication whatsoever between players. After the game, everyone gets a checkbox next to their name, you check the box to add them as a rival (rivals work like friend codes in that both parties have to be willing. However, rivalries are different than friendships in that there is not in-game communication). OR: you can enter someone's friend code (or gain rivals) and play a "Friends and Rivals" game. Any of your friends or rivals can join a game which you host (or they host, you can choose to block freinds or rivals), in a friends and rivals game, there are more options as far as gameplay, and only for friends there is text and voice chat before and after the game.
In MarioKart to find your friend code, go to:
Nintendo WFC, Friend Code; then to see your friend code, touch confirm friend code; to enter someone else's code, touch register friend code.