Like other ships, a submarine can float because the weight of the water that it displaces is equal to the weight of the ship. This means the pressure of the submarine pressing down on the water is the same as the pressure of the water pushing up on the ship.
In order to control buoyancy, the submarine relies on special tanks that can be filled with water or air. To return to the surface, the tanks are filled with air. This makes the submarine less dense than the water around it, causing the sub to float to the surface.
When the captain decides it's time to take a dive, the air is released through a vent, and the tanks are flooded with water. This makes the submarine denser than the surrounding water, which causes it to sink.
The air we breathe is made up of four gases: nitrogen, oxygen, argon, and carbon dioxide. When we take a big breath of air, our bodies use up the oxygen. When we breathe out, we exhale carbon dioxide. We don't do anything with nitrogen or argon.
Imagine you are about to be sealed in a giant tube with all your friends. When the tube is sealed, all the air available to you is now inside. There's just one problem: no air can get in or out. A submarine works the same way.
In order to keep people breathing (and alive!), three things must happen. First, oxygen must be pumped into the vessel as the crew uses it up by breathing. Oxygen is typically supplied to the submarine from pressurized tanks. A computerized system monitors oxygen in the air and releases fresh oxygen as its needed by the crew.
Finally, exhaled moisture from breathing must be removed to prevent condensation on the interior of the ship or equipment. This is done with dehumidifiers.
Most ships have special equipment that can remove the salts from seawater to turn it into fresh drinking water. This process is called distillation. Some submarines can produce up to 40,000 gallons of fresh water per day. It is used for everything from cooling electronic equipment to cooking and showering.
Since submarines are metal, they conduct heat from inside the ship to the cooler surrounding water. In order to keep the crew comfortable, submarines also need heating systems.