The flow of saliva slows down during sleep. As a result, mouths don't get the same level of oxygen as they do while we're active. This allows anaerobic bacteria, which don't need oxygen, to thrive. The waste products from these bacteria often contain sulphur - and those compounds of sulphur are what we smell.
Onions are also culprits for this type of bad breath because they contain sulphur. Mucus in your nose can thicken while you sleep, and your tongue falls to the back of your throat - both of which provide welcome environments for anaerobic bacteria.
Treatments for bad breath abound, however the two most common solutions are to brush your tongue to remove excess plaque, and to use an antimicrobial mouthwash.