When you put something, say a bottle with a message, in a liquid, the bottle tends to sink. To do this it must push some of the liquid out of the way. This sets up a force in the liquid to oppose this.
If something is very dense, like a stone, the force set up in the liquid is not enough to stop the stone pushing it out of the way. But a bottle with a message in it is actually partly stone (the glass) but also a lot of air. The combined water and glass is actually not very dense, so the force of the liquid can easily balance the bottle, and it floats. This is also why ships, which are made of steel, also float. The ship contains a lot of air inside its hull, so it's just like a bottle.
If you put something in water, like salt, the salt will dissolve. That makes the water more dense, so it will hold up heavier things. That is why a ship will float higher in the sea than in a river. This was a problem that caused the Plimsoll Line to be invented. A ship loaded in a seawater port would be likely to sink in a freshwater river unless you know how much to load it.