Stool sinks or floats depending on how much air (gas) it contains. If there is little or no gas trapped in the stool, it sinks. If it contains a lot of tiny gas bubbles, it floats. The amount of gas in your stool is affected by the foods you eat, the types of bacteria that normally live in your gut, and how long it takes food to move through your system on its journey into the toilet bowl.
We used to think it was the fat content of stool that predicted whether it would sink or float, but scientists thought of a clever experiment to sort it out. They used special equipment to remove all the air from stool, and it sank every time, no matter how fatty it was.