It's a matter of air pressure that acts on the water. When the straw is uncovered, there are two forces acting on the water inside. There is gravity, pulling the water down, and air pressure. The pressure is about the same on both ends of the straw, but on the top of the straw, the pressure pushes the water downward, and on the bottom of the straw, the pressure pushes upward. Both of the pressures are the same, so the net force is just the force of gravity pulling the water down and out of the straw. When you cover the top of the straw, you block the air from pushing down on the water. In this case, the only forces on the water are the air pressure pushing up, and gravity pulling down. However, the air pressure is a greater force than gravity and keeps the water in the straw despite gravity's pull.
Work Step by Step
When air pressure is stronger than the force of gravity, a substance is able to work against gravity such as in this example.