Submarines are among the most impressive large-scale machines ever built. Unseen, they explore the dark depths of the world's oceans with only electronic eyes and ears to guide them. Let's see how they work.
The name of the game in diving and surfacing is buoyancy. When an object is put in the water, if it displaces less water than the object's volume, the object floats or has positive buoyancy. If the object weighs more than the water displaced by its volume, it sinks or has negative buoyancy. This is why a piece of cork will float and a piece of metal will sink.
Submarines contain ballast tanks. The Blueback has six of them. When these tanks are mostly filled with air (as indicated in the picture by the grey sections at the fore and aft ends of the submarine), the submarine has positive buoyancy and floats.
Hear the dive alarm! (21k WAV file)
For a submarine to dive, it first opens vents at the top of the ballast tanks. At the bottom of the ballast tanks are flood ports to the sea that are always open. When the vents are opened, air rushes out the top and water flows into the bottom of the ballast tanks. This makes the submarine heavier and heavier until it has negative buoyancy. At this point, the submarine dives. The vents are then closed to prevent the submarine from losing any more buoyancy.
When the submarine needs to surface again, pressurized air is released into the ballast tanks. This forces water out the flood ports at the bottom of the ballast tanks and makes the submarine lighter and lighter until positive buoyancy is achieved. At this point the submarine rises upwards and surfaces.

When a submarine needs to surface in an emergency it uses a special bank of high pressure air that can push all of the hundreds of tons of water in the ballast tanks out in a matter of seconds, as shown for example in the movie The Hunt for Red October (in which the torpedo room of the Blueback also made a special appearance as one of the settings onboard the Red October.)
-Image courtesy of Paramount Pictures.