A submarine or a ship can float because the weight of water that it displaces is equal to the weight of the ship. This displacement of water creates an upward force called the buoyant force and acts opposite to gravity, which would pull the ship down. Unlike a ship, a submarine can control its buoyancy, thus allowing it to sink and surface at will.
To control its buoyancy, the submarine has ballast tanks and auxiliary, or trim tanks, that can be alternately filled with water or air (see animation below). When the submarine is on the surface, the ballast tanks are filled with air and the submarine's overall density is less than that of the surrounding water. As the submarine dives, the ballast tanks are flooded with water and the air in the ballast tanks is vented from the submarine until its overall density is greater than the surrounding water and the submarine begins to sink (negative buoyancy). A supply of compressed air is maintained aboard the submarine in air flasks for life support and for use with the ballast tanks. In addition, the submarine has movable sets of short "wings" called hydroplanes on the stern (back) that help to control the angle of the dive. The hydroplanes are angled so that water moves over the stern, which forces the stern upward; therefore, the submarine is angled downward.
To keep the submarine level at any set depth, the submarine maintains a balance of air and water in the trim tanks so that its overall density is equal to the surrounding water (neutral buoyancy). When the submarine reaches its cruising depth, the hydroplanes are leveled so that the submarine travels level through the water. Water is also forced between the bow and stern trim tanks to keep the sub level. The submarine can steer in the water by using the tail rudder to turn starboard (right) or port (left) and the hydroplanes to control the fore-aft angle of the submarine. In addition, some submarines are equipped with a retractable secondary propulsion motor that can swivel 360 degrees.
When the submarine surfaces, compressed air flows from the air flasks into the ballast tanks and the water is forced out of the submarine until its overall density is less than the surrounding water (positive buoyancy) and the submarine rises. The hydroplanes are angled so that water moves up over the stern, which forces the stern downward; therefore, the submarine is angled upward. In an emergency, the ballast tanks can be filled quickly with high-pressure air to take the submarine to the surface very rapidly.
Taken From: http://science.howstuffworks.com/submarine1.htm