How are ships are prevented from sinking?
There are two things that keep a ship from sinking. The first is in proper design, and the second is in having proper safety measures.
Pranitingale is right, the main principle at work is named for Archimedes. It's kind of complicated, but it's a combination of the shape of the boat (lots of surface area to push down on the water) and the fact that boats are not made of solid metal (there's lots of air in them.) As long as the weight of the boat and its cargo is less than the weight of the water the boat is pushing away, the boat will float. If the boat and its cargo are heavier than the water being pushed away under the boat it will sink. Water is pretty heavy stuff.
Think about the difference between throwing an empty soda can into the ocean and a rock. The can is made of lightweight material, has a lot of surface area touching the water, and is full of air. The rock is made of a heavy material and has no air in it. It's very heavy for its size. The amount of water it pushes away doesn't equal its weight, so it sinks.
There is also the shape of the boat to consider. They are built low to the water and for maximum area. You don't see boats built like skyscrapers. Even big cruise ships get wider when they get taller.
The other side of the coin is more to do with safety. Ships can be prevented from sinking, if damaged, with water tight compartments. Remember, water weighs quite a bit, so if your boat "springs a leak" it will gain weight quickly and sink. But if your boat is separated into "sections" that can be closed off, it is possible to seal a compartment to limit the amount of water coming in. Titanic had these compartments, but unfortunately too many of them were breached and the weight of the water continued to increase.
Pumps are also used to prevent ships from sinking. If it is possible to remove water as quickly as it is coming in, or near that rate, it may be possible to get the ship to port before the weight increases too much.
The best thing that keeps ships from sinking is design. If a ship is designed properly from the get-go it has a much better chance of surviving the perils of the sea.