The n is what's known as the accusative case, which goes on the part of the sentence which is the object. The whole sentence would be "Mi deziras al vi bonan nokton" so the good night part is the object of the sentence and therefore gets the n. It allows you to play with the word order of the sentence but retain the same meaning.
Night: 'The period between sunset and sunrise, when a location faces far away from the sun, thus when the sky is dark.' Esperanto = Nokto
Evening: 'The period of time at the end of the day, usually from about 6 p.m. to bedtime.' Esperanto = Vespero
Nokto is the subject form. Nokton is the accusative, or object form. The full sentence is Mi deziras al vi bonan nokton” or “I wish to you a good night”. The “good night” is what’s being wished, so it is the object of the sentence and thus is in the accusative case. All nouns in Esperanto work this way, where the singular subject ends in -o and the singular object ends in -on.