Fish swim to the top of the water line in order to search for oxygen if the oxygen content in the water is too low. If pet fish are doing this, it usually means that they are in distress and are not finding sufficient amounts of oxygen in their own environment.
One of the most common causes of this problem is related to dirty water within the fish tank, which is most commonly caused by overfeeding. If a dead fish is spotted in the tank, it should be removed immediately, as this can also cause water pollution issues. Finally, all filters, air stones and bubble wands should be inspected, as poor performance of these components can affect the entire filtration system.