Image of the heart and lungs courtesy of 20th US edition of Gray's Anatomy of the Human Body.
The principal function of the lungs is to transport oxygen from the atmosphere into the bloodstream, and in turn release carbon dioxide from the bloodstream into the atmosphere. This exchange of gases is made possible by special cells that form millions of tiny, exceptionally thin-walled air sacs called alveoli.
Image of lung tissue captured under a biological microscope with the Jenoptik C14+ microscope camera.
The lungs also take on some tasks outside of respiration. They alter the pH of blood by altering partial pressure of carbon dioxide. The lungs filter out small blood clots from veins. They even sometimes serve as a layer of soft shock-absorbent protection for the heart.