Picture yourself enjoying a night out on the town that has, unfortunately, come to a thrilling but exhausting end. Your friends have graciously delivered your slightly intoxicated self to your doorstep, and you send them off with kind words. At this precise moment, you experience brief clarity and decide that you must rush to your laptop to chronicle the exciting evening including your final farewell.
If, in your parting statement, you meant to convey “Have a good night,” then you would type it as two words, good night.
However, if your last comment was a benevolent wish upon your friends, you would type goodnight.
Confusing? Not really. Just keep in mind that the first is an abbreviated form of “Have a good night” and the second is a pleasant wish bestowed.