Not surprisingly, the word 'afternoon' means 'after noon'. This much is obvious. So up until midday (noon) you say 'Good morning', then after noon you say 'Good afternoon'. That's what 'afternoon' means.
But more importantly, you should note that 'Good night' is NOT a greeting. We never say this when we meet someone, however late it is. Even if you meet someone at midnight, you don't say it. 'Good night' is only used as a farewell, when you are parting company from someone for the last time that day. You say 'Goodnight' when you are leaving somewhere late in the evening, or going to bed.