A cough is supposed to protect you. It gets out stuff that doesn't belong in your lungs and windpipe, like inhaled dirt or food. Here are the common triggers.
Viruses. Colds and the flu are the most common causes. While annoying, coughs that are “productive” get germy mucus out of your lungs when you're sick. Most will go away in a few days. After a cold, though, some "dry" coughs last weeks or months. That could be because coughing irritates your lungs, which leads to more coughing, which irritates your lungs, and so on ...
Allergies and asthma. If you have them, inhaling a trigger like mold can cause your lungs to overreact. They're trying to cough out what’s bothering them.
Irritants. Even if you're not allergic, things like cold air, cigarette smoke, or strong perfumes, can set off a hacking spell.