How to make a safe in vanilla Minecraft that locks and unlocks with a key?
This is a bit difficult to answer since (without mods) you're limited to the in-game rules of Minecraft, which are very lenient. Even if you where to build a very advanced contraption, someone would still get around it by digging/breaking a hole in the wall and just bypassing any locks.
With that said, Here's the easiest setup I can think of.
Make an Iron Door and a Red Stone Torch. Iron doors require red stone power to open, and will close if there's no power to the door. As such, the door will always be closed. When you come home, place a Red Stone torch in front of the door (to open it) walk inside, turn around and hit the torch again to break it. (which closes the door behind you.)
This is the simplist way to create a "key" assuming no one else knows how to operate red stone.
Side note: Unless you go into some advanced Red Stone or command block scripting, You can't have a "secure" door. The best way to keep a place secure is to keep it hidden. For example, Maybe the door into the house is below the house itself? Maybe the house is inside of a cave that's dark except for torches you've put up before hand? The only way to make something secure without mods is to make it not be seen in the first place.
Unfortunately, there's no way to keep someone out of a room if they know it's there and both parties are in survival mode. Every placeable block can be punched through, any lava can be blocked off with dirt, and it's very difficult to make an invisible trap (not impossible, but probably qualifies as advanced redstone). So, the best way to make a vault is to try and hide it as well as possible. This means that the entrance should be hidden and the vault shouldn't have any walls, roof or floor next to a visible building (since people can mine through that building and accidentally mine into the vault). With that in mind, I usually prefer to use piston doors - they are easy to hide and easy to make. If you have a corner to work with, you can simply use two sticky pistons to retract the corner wall. An easy way to hide a button is behind falling water, since you can click through it but it's difficult to see. Use a button with lots of repeaters delaying the signal to give yourself time to get over to the entrance before it closes, or just use a lever if you don't mind closing it before you log off.
There are two very simple ways I know of to "lock a door". (or vault) Of course, these both assume that the gamemode is adventure and other players cannot grief it down. Otherwise, you would need a special plugin/mod to stop this.
Idea 1: Use an iron door. (Like what was mentioned in the comments.) When the lever is powering it from the inside, it is always open, when the person on the inside turns off the lever, the door is closed and cannot be opened from the outside.
Idea 2 If you want a wooden door, you can use the painting trick. This is where you place a painting over the door. It not only hides the door, but also essentially locks it. If the door is open, one can pass right through the painting into the room. Once inside the room, they can close the door behind them, without griefing the painting, the person on the outside cannot pass through or open the door.
Alternatively, you could simply hide the entrance. This may be your best option for factions/pvp/raid servers where players can grief and may be trying to get into your bases and buildings.