From the Super Mario Wiki
Keys are items used for various purposes that appear in many games of the Mario, Yoshi, and Wario series and related media. Though their appearance and usage varies, they are typically used to open a locked door or a secret path.
Super Mario series
Super Mario Bros. 2
Super Mario Bros. 2 is the first Mario game to feature Keys and locked Doors. All keys in this game are guarded by Phantos, one of which will chase the player until the key is dropped or used to unlock a door. Unlike many other items in the game, Keys are always visible without having to be rooted up like a Vegetable.
Most keys are out in the open, but at least one was guarded by a Birdo in World 7-2.
Super Mario World
In Super Mario World, Keys and Keyholes can be found in various levels. Bringing a Key to a Keyhole activates the respective level's secret exit. Like other items in the game, Keys can be carried around and dropped or kicked upwards, but they will have no effect on a Keyhole unless held by the player. Yoshi can carry a key in his mouth, but will swallow it if it remains in his mouth for too long. The location of a Key does not reset when it is scrolled off the screen.
Super Mario 64/Super Mario 64 DS
Keys appear in Super Mario 64 and its remake, Super Mario 64 DS. There are only two keys in the game and they can found in two Bowser's levels: Bowser in the Dark World and Bowser in the Fire Sea. Mario gets them when Bowser is defeated. The keys can open Doors of the Princess Peach's Castle. The first key opens the door of the castle's basement and the second key opens the door of the castle's upper floors. If a player attempts to open the door to the upper floors with the basement key, a message will appear to let the player know that that key doesn't fit the lock. Also, a beta key appears in Big Boo's Haunt level.
In Super Mario 64 DS, keys retain their purpose from the previous game, but there are more other kinds of keys. First, there are those that Rabbits steal. The first key Yoshi has to obtain unlocks Princess Peach's Castle; only after he catches that first rabbit can he get inside. There are many other rabbits that can be found in the game, but they only unlock minigames. The second kind of keys the player must collect are held by three bosses (Goomboss, King Boo, and Chief Chilly) , and are used to free Mario, Luigi, and Wario.
Super Mario Galaxy
In Super Mario Galaxy, keys can be collected to automatically open locked doors or force-fields. They are found in galaxies such as the Gateway Galaxy and the Ghostly Galaxy. In the Gateway Galaxy and Ghostly Galaxy, a Grand Goomba and a Boo, respectively, each hold a key which is released after Mario or Luigi defeats the enemies.
New Super Mario Bros. series
In New Super Mario Bros., at the end of each World from 2 to 7, Bowser Jr. places a random boss and if the player wins, a Key will be granted. In New Super Mario Bros. Wii and New Super Mario Bros. 2, when Mario defeats any of the Koopalings at their castles, he'll receive a key that he must grab to exit the castle. This item doesn't serve any other purpose in both games though.
Super Mario Galaxy 2
Keys also appear in Super Mario Galaxy 2, and they serve the same function they did in Super Mario Galaxy. They can be found in galaxies such as the Haunty Halls Galaxy and the Battle Belt Galaxy. They also briefly appear in the Rolling Masterpiece Galaxy.
In Super Mario World 2: Yoshi's Island, its remake and its sequel, Keys can be collected and carried like Yoshi Eggs, taken up one space of the player's maximum number of Eggs. They can be used to unlock Mini Battle houses, locked doors in castles and Corks that block pipes. Additionally, during the cutscene following the defeat of a castle boss, a large key appears and unlocks a keyhole.
Wario Land series
Wario Land: Super Mario Land 3
In Wario Land: Super Mario Land 3, the player can sometimes find a key inside a Face Block. These keys are hidden throughout many stages of the game and they are required to open the door to a treasure chamber. The keyhole in that the key fits is always found in the same stage as the key and the key can not be taken out of the stage, so it has to be recollected after leaving the level. Inside of the treasure chambers, Wario can find one of the collectible Treasures. However, he cannot be Tiny Wario as he needs to body slam the chest to open it (although breathing fire works).
Wario Land 3
While being absent in Wario Land II, keys reappeared in the game's sequel Wario Land 3. There, four differently coloured keys were hidden throughout the level, namely a grey, a red, a green, and a blue one. Wario can only clear a level if he finds one of the keys and manages to take them to their respective Treasure Chest. By doing so, he earns himself a new treasure that helps the player to proceed and find new levels. Usually, not all keys in a level are available from the start. The player has to obtain new abilities and meet certain requirements to find them all. After a key is taken to a Treasure Chest, the chest gets replaced by a Goal Door.
Wario Land 4
When Wario Land 4 was in development, Wario originally needed a key to unlock the four boxes (much like Wario Land 3) to gain the four Jewel Pieces. In the final game, Wario merely has to touch the box in order to open it. Besides the Keyzer, no other key is seen in the game.
Obtaining four keys is a critical component to freeing Donkey Kong.
Keys make their first appearance in Donkey Kong for the Game Boy in stage 1-1, after stage 0. In all regular levels of the game except for stage 0 (which has the stages from the original Donkey Kong arcade game), Mario has to find the key and take it to the correct locked door. Mario picks up the key by standing on it and lifting it above his head. Often, the key has to be thrown upwards because Mario cannot hold it while climbing a ladder, and it can also be used to take out enemies by throwing it.
In Donkey Kong Country Returns, there is a key that can be purchased from Cranky Kong's Shop in each world, for a total of eight keys. Each key costs twenty Banana Coins, and when purchased, unlocks an alternate route on the world map that will allow Donkey Kong and Diddy Kong to reach additional levels.
There are many key designs in Paper Mario and its sequel, but each only opens doors in the area they are found. There are Castle Keys for every castle and fortress, and various others keys in different locations. In Paper Mario, they were used in Tubba Blubba's Castle, Bowser's flying castle, and Peach's Castle. Tubba Blubba's keys look like three leaf clovers, while Bowser's look like a gold three leaf clover with a blue ball in the middle. There are about three of Tubba's keys and five of Bowser's keys. Peach's Castle Keys are pink versions of Bowser's. Also, a living key called Yakkey locked the Gusty Gulch Windmill.
Paper Mario: The Thousand-Year Door
Super Paper Mario
In Super Paper Mario, keys appear much more prominently than in the previous installments. They are used in almost every level to open locked doors. Each place has a different type of key.
In Luigi's Mansion, Luigi had to collect many keys before he could advance to other rooms. They were usually found in Treasure Chests, although some could be found right out in the open. Also, there are four special keys, with each one having a different card suit: heart, club, diamond, and spade.
Luigi's Mansion: Dark Moon
Keys return in Luigi's Mansion: Dark Moon, with a different design. They're again used to open doors and any key can be used to unlock a door. In the Old Clockworks, a key can be generated from a gold pot.
For an unknown reason, perhaps the mansion's machinery, a key is turned to dust after being used to open a door. The only exception for this rule is the Front-Door Key of the Treacherous Mansion.
Mario Party series
Bowser owns various keys in the Mario Party series. These so-called Bowser Keys are usually found in Bowser Minigames, and are necessary to escape from Bowser. In earlier Mario Party games, Bowser Keys are simply keys shaped like Bowser's head. In later games, they are engraved with Bowser's likeness. The minigame Locked Out from Mario Party features keys with key heads shaped like mushrooms, flowers and stars.
Super Smash Bros. Brawl
In the Subspace Emissary mode of Super Smash Bros. Brawl, Emissary Keys are found somewhere near locked gates. If the player finds a key and tries to go through the gate while having it in his or her hand, the gate will become unlocked.
Super Smash Bros. Brawl Trophy
Names in other languages