Respiration—gas exchange between blood and an outside medium (air or water)—always takes place across a wet membrane whether the organ hosting the blood is a gill or a lung. There is no fundamental difference between gills and lungs. Their essential anatomy is the same: a large surface area overlaying blood vessels. To further increase the surface area for efficient gas exchange, gills are usually branched or filamentous and lungs may be invaginated or branchiate.
A gill is not necessarily restricted to an aquatic medium: if it can stay erect and wet, it will also function in the air. Likewise, a lung can also function in water. In fact, in certain habitats animals with gills can live side by side with animals with lungs.
Here is an example. In this picture are 2 species of tiny snails that I found on a rock that I had pulled out of the sea during the high tide at a Florida beach. The 4 snails with shiny shells are Assiminea succinea, a species in the superfamily Rissooidea. They have small rudimentary gills. The snail with the duller shell near the center-left is Pedipes ovalis in the family Ellobiidae. Like its distant relatives the pulmonate land snails, Pedipes ovalis obtains its oxygen via a lung.
We see that in transitionary seashore habitats snail species with gills and lungs live alongside each other, creating an evolutionary mosaic.