When an object is placed in a fluid, the fluid exerts an upward force we call the buoyant force. The buoyant force comes from the pressure exerted on the object by the fluid. Because the pressure increases as the depth increases, the pressure on the bottom of an object is always larger than the force on the top - hence the net upward force.
The buoyant force is present whether the object floats or sinks. Let's consider a floating object, but the analysis is basically the same for a submerged object.
We'll also consider a rectangular block, although a similar (more complicated) analysis leads to the same result for funny-shaped objects.
The object experiences forces on each of its six sides. On each side, the force is the pressure multiplied by the area of the side, and is directed perpendicular to the side and toward the inside of the object.
The force on the left side is tricky to calculate, because the pressure is different at different levels. Fortunately we don't have to calculate it because this force is equal-and-opposite to the force on the right side. Similarly, the forces on the front and back cancel.
For a floating object the force on the top surface, of area A, is directed down and is equal to:
Ftop = Patm A
If the bottom of the object is a depth h below the surface, the force on the bottom is directed up and is equal to:
Fbottom = (Patm + ρgh) A
The net force, which we call the buoyant force, is directed up and equals:
Fb = Fbottom - Ftop = ρgh A
hA = the volume of fluid displaced by the block (the submerged volume)
Multiplying the volume of fluid by the density of the fluid, ρ, gives the mass of the displaced fluid.
Fb = mdisp g
This is known as Archimedes' principle: the buoyant force is equal to the weight of the fluid displaced by the object.
When an object floats, the buoyant force balances the force of gravity. When it sinks, gravity wins.