It's funny how some topics elicit a large response.
Mucus, as pointed out earlier, originates in the airway and nasal cavities, is composed primarily of long glycoproteins (MUC proteins) and water in addition to lots of small signalling molecules (ATP, cytokines) and ions (a lot of sodium and chloride). The mucus is regulated by the body- a symptom of cystic fibrosis is dehydrated mucus due to faulty ion transport.
The function of mucus is protective- the lungs must remain sterile in the face of inhaled contaminants- bacteria, mold spores, virii, particulate matter, yeast, etc. The mucociliary system serves to transport the junk out of the lung. Some bacteria (pseudomonas) thrive in the dehydrated mucus, leading the chronic lung infection- this is usually what kills the CF patients.
The mucus is secreted and transported by the motile cilia covering the airway and nasal cavity. Here's where it gets interesting: the mucus layer (in normal situations) is a constant thickness. Since the surface area of the lung decreases as you move up and out, the mucus must get absorbed, or components must get absorbed to decrease the volume. It's not clear how the mucus layer is maintained at a constant thickness.
Lots of things can go wrong with this system- excess secretion, improper transport, decreased resorption. That, coupled with inflammation of the epithelial layer (from the cytokines that are secreted into the mucus), can cause additonal problems.
A rule of thumb- if your mucus is cloudy, you have a bacterial infection. If it's clear, you have a viral infection. This is based on the size of the bug (light scattering).
What if it starts out nice and clear and thin, and then you wake up and its turned green and goopy?