This might be one of the most confusing questions that have been bothering a common man. The general idea is that because a man is alive he can make himself stay above the water whereas a dead person has no abilities, therefore, he has to sink. But that isn't the case and there is a perfect explanation for this phenomenon.
A person only starts to sink into the water when the air in his lungs is replaced with water. Once the body is submerged under the water, then the body stays under the water until the bacteria in the gut and the cavity in the chest can produce enough gases such as methane, hydrogen sulfide, and carbon dioxide in order to float on the surface of the water like a balloon. The building up of these particular gases in a human body can take up to weeks sometimes, also depending on various other factors.
When the body is submerged then it does not necessarily mean that all the body parts are inflated at the same time. First, the torso part of the body which consists of most of the bacteria get bloated more than the head and the limbs. The process of buoyancy comes into play now. The parts of the body which are most buoyant rise first, leaving the head and the limbs to be dragged from behind the chest and the abdomen. Since the limbs and the head can only drape forward from the body, the corpses tend to rotate such that the torso floats face-down with the arms and legs hanging under it.
Most of the dead bodies float this way, but there are also exceptions. The smaller the hands and legs are, then the more likely it is for the corpse to float facing up. If a body stays on the surface of the water for a long time then it will release the gas that been built up and then it will sink into the water once again. Decomposition continues underwater and then more and more gas is collected and the body may start floating again. This is also known as a “re-float” in terms of rescue workers.