Cough (or irritant) receptors are scattered throughout your airways. When they get stimulated (by irritants like dust or allergens, or triggered by an accumulation of excess mucus), they send a signal to the cough center in the brain.
Your brain processes this information and then tells certain respiratory muscles to rapidly contract, which results in a cough.
Coughing drives fast-moving air from the lungs, which causes your airways to vibrate. This vibration loosens the mucus that’s stuck to the mucus membranes in your airways. Coughing propels this mucus upwards and out of your airway.
While you have the power to control your cough (for example, when you cough on purpose), an involuntary cough has reflexive power–meaning it will happen even when you don’t want it to, like when you’re sick with a respiratory tract infection.