The lungs are responsible for gas exchange between inhaled air and blood. They have a branching structure from the trachea to bronchioles and alveoli where oxygen and carbon dioxide diffuse between the air and blood. Muscles like the diaphragm and intercostals expand and contract the lungs and chest cavity to inhale and exhale air, bringing oxygen into the body and removing carbon dioxide through diffusion based on partial pressures and concentrations.