Ships float because they displace enough water for the buoyancy force to be greater than the force of gravity acting on the ship. The displaced water attempts to return to its original position, now occupied by the ship, which pushes the ship up. This effect is known as buoyancy force. The strength of the force of gravity pulling down on the ship is affected by its weight. Whichever of these forces is stronger determines if the ship floats or sinks.
The buoyancy force is explained by the Archimedes principle. Any object, wholly or partially immersed in a fluid, is buoyed up by a force equal to the weight of the fluid displaced by the object. Clever ship design allows very large, very heavy ships to float, simply by displacing an amount of water of greater weight than the ship itself. Once the ship's hull has been designed to take this into account, adding more weight, for example cargo, increases the gravity force acting on it which causes it to dip lower into the water, displacing more water and increasing the strength of the buoyancy force. The two forces cancel each other out and the ship floats. This balance of forces is known as a state of equilibrium.