Just as we can dive under water and hold our breath for a limited time, so can fish when out of water. For their size they can live quite a long time out of water, 5 minutes or more for a 5" fish as I remember from my fishing days. Some also absorb some oxygen through their skin and can travel overland to other pools/water.
There are some fish that have actual lungs and can breath air like a mammal, often in conjunction with gills. They usually live in soupy water with a low oxygen content.