The easiest way to understand the science involved is to float a metal bowl.
While the metal itself is heavier than water and would normally sink like a stone, the shape spreads the weight over a large area.
So, while the metal’s weight causes the bowl to sink somewhat, the bowl’s rim is high enough that the bowl will not be totally engulfed by the water. A ship is not unlike a large bowl.
The surface area is much larger than you’d expect if you only see the top portion sitting above the water.