Submarines get the air needed when underwater by pumping oxygen inside the submarine.
The submarine contains oxygen tanks with enough oxygen in them to last the entire voyage on the submarine.
The oxygen tank system is computerized and the computerized system in the submarine monitors the oxygen levels in the submarine and then releases more oxygen as it's needed to the people in the submarine can breathe.