なに (nani) is used when the kanji stands alone or at the beginning of a question.
なん (nan) is used before certain sounds (like が, に, じ) in compounds like 何月 (なんがつ, nangatsu: what month), 何人 (なんにん, nannin: how many people), and 何時 (なんじ, nanji: what time).
The reading changes from なに (nani) to なん (nan) for easier pronunciation before certain consonants.
Etymology: Picture of a person carrying a heavy bag → we ask what is inside → what
Examples
Kun-yomiNative Japanese Readings
何ですか。
What is it?
Uses kun-yomi なに (nani) when the kanji stands alone meaning what
ExceptionIrregular Readings
何月ですか。
What month is it?
Exception: 何 is read as なん (nan) in 何月, which changes from なに (nani) to なん (nan) before certain sounds
何人ですか。
How many people?
Exception: 何 is read as なん (nan) in 何人, which changes from なに (nani) to なん (nan) before certain sounds
何時ですか。
What time is it?
Exception: 何 is read as なん (nan) in 何時, which changes from なに (nani) to なん (nan) before certain sounds