Breathing is controlled centrally by a group of specialised respiratory nuclei located in the brainstem. These neurons are influenced by impulses from the peripheral and central chemoreceptors, which respond to changes in the oxygen and carbon dioxide tension of the blood, as well as pH levels. The respiratory nuclei then coordinate the muscles of respiration accordingly.
On inspiration, the diaphragm and external intercostal muscles contract, expanding the intrathoracic volume. This reduces the intrathoracic pressure causing air to be sucked into the lungs. Think of the analogy of a bellow.
On expiration, the opposite happens. As the muscles relax, the intra-thoracic volume decreases and the pressure increases, pushing the air back out of the lungs.
The respiratory centre in the brainstem, which regulates this rhythmic pattern, receives additional input from a variety of other receptors which help to ‘fine tune’ the overall performance of the system.