My answer to: (To vote and comment on Quora, visit the link here.)
25 of the Sanskrit/Hindi consonants are divided into five groups of five consonants each, depending on the location where the sound comes from.
These groups are called कवर्ग, चवर्ग, टवर्ग, तवर्ग, पवर्ग. The last consonant in each of these groups is the nasal sound from that particular location of sound. These are: ङ ञ ण न म.
When the nasal sound comes immediately before a non-nasal sound, the letter used to represent the nasal sound should be the one that belongs to the same group as the non-nasal sound.
For example, in the word “हिन्दी”, the consonant द is preceded by the nasal sound, so we look for the nasal consonant of तवर्ग, in which द lies. That nasal consonant is न. So the word is written as हिन्दी. Similarly, other words with the nasal sounds are:
गङ्गा, चञ्चल, घण्टा, गम्भीर
These versions of these words are not accurate in Sanskrit, as each sound has not been represented by a unique letter (the anuswara is ambiguous):
गंगा, चंचल, घंटा, गंभीर
The anuswara is supposed to be used only when the nasal sound precedes a consonant that is not one of the 25 grouped consonants (य र ल व श ष स ह).
For example: संसार, संहार, etc. are correct with the anuswara.
I think CBSE has recently recommended rules for Hindi spelling in schools in the modern context. It has recommended that the anuswara be used to represent the nasal sound, and that we do not go by the original Sanskrit rules. Personally, I dislike the recommendation and prefer the classical spelling.
Disclaimer: I am not entirely sure about this, as I can’t find any supporting links right now. The information about the CBSE recommendation is based on faint memories of having read something like that some time in the past.