"Good night" vs "goodnight" (vs "good-night")
Looking at the ngrams of both, good night has a more frequent use. Goodnight has only appeared recently, suggesting that it has been introduced – but is correct to use.
I would write "Goodnight, sweetheart" and "Have a good night".
I also think that "goodnight" is just a shortened way of saying "have a good night".
Phrases can confuse us because they are wrongly used, and also because they use the very same word pairing. "Everyday" is a perfect example. Sometimes a merchant's sign reads, "Everyday low prices." But why would a merchant voluntarily associate him/herself with banality? No, it must be that the writer-merchant meant that the prices are low every day of the week! He/she should've written "Every day low prices." Goodnight is surely one word when it is rendered as a single, concise bidding: "Goodnight!" And this practice (of writing it as one word) has probably come into standard English because "goodbye" is a single word (albeit it represented a four-word sentiment originally: "God be with ye," from a time when "ye" was used to mean you).
Have you consulted a dictionary?
Merriam-Webster lists good night and good-night. Collins, AHDEL, and others list only good night. Oxford online lists goodnight.
Looks like you have your pick. In AmE, I most often see good night.