How Heat Moves
In order to melt, ice needs to absorb heat. Heat is transferred in three ways: through conduction, convection and radiation. Only the first two are important in understanding how a cooler works. Conduction is when heat spreads from one object to other objects which are physically touching. The molecules of the hotter object are moving faster, and when they bump into adjacent molecules they transfer some of their heat energy. Convection is heat that moves through air currents. A stream of air moving past a warm object will continuously transfer heat away as it passes. Similarly, a stream of air moving past a cold object will transfer heat into it.
The cooler minimizes conduction by using insulators. Insulators are materials they do not conduct heat very well. Most coolers use Styrofoam as an insulator. Styrofoam has small pockets of air trapped inside of it. Gasses such as the ones that make up air have low density, which makes them poor conductors. The bubbles inside the Styrofoam are too small for convection currents to form, so they can't transfer heat that way either. A little bit of heat does flow through the Styrofoam, but it flows very slowly allowing a small amount of ice to keep the inside of the cooler at freezing temperature for hours.
All this insulation only keeps the cooler cool while it is closed. When it is open, warm air currents can flow into the cooler, melting the ice fairly rapidly. That is why people usually try to keep their coolers closed as much as possible. Even when the cooler is closed, convection can help warm up the ice. Warm air currents can warm the Styrofoam somewhat, but only a little. Many coolers can keep their contents ice-cold for days if they are kept closed.