It is because of what is called 'buoyant force'.
If you take a mug and try to push it inside water, you can feel a force that pushes the mug upwards. That is buoyant force.
F = v x d x g, where
F = buoyant force,
v = volume of the body,
d = density of the liquid,
g = acceleration due to gravity
Thus, in this case which you have mentioned, buoyant force primarily
depends on the volume and density. More the volume, more the force. But
more the volume, less the density.
Ships are in fact made of iron, steel and other metal alloys which are
in fact much denser than water. But the air spaces in them increases the
volume to such an extent that the overall volume increases, and the
density drops (more the volume, less the density). Thus, the buoyant
force also decreases, and ships float.
Human body density is in fact less than water. But the problem is that
when people fall into water, they consume a lot of water which keeps the
volume steady but increases the overall density. Thus, they sink. If
one doesn't consume water at all, then he/she stays afloat in water.