@tridacna has provided one of the correct answers, in that there is a silicone release agent on the mask glass at manufacture, which can be removed with mildly abrasive toothpaste (not gel type), or by the flame method, or some people even recommend leaving coke in the mask overnight (not tried that method) I flame a mask to get off the majority and then apply toothpaste. YMMV
In use, you will get a build up of body oils, which also cause fogging, so a mild detergent used on the mask and seals with warm water should remove those contaminants.
When cleaning it's normally the edges next to the seal which still have contaminants remaining. I use a soft old tooth brush to get into those more difficult areas (and to clean the tooth past off afterwards)
Also once cleaned and dry, I breath on the mask to see if any areas still fog.
Because I frequently dive, I re tooth paste my mask (or give it a really good clean rather than a rinse) 3 or 4 times a year)
For diving I use a thin smear of baby shampoo. I have found that if rinsed with fresh or drinking water the mask might sometimes fog a little on occasion, but if rinsed with sea water it doesn't.
I have NO idea why this is, but I generally roll in with the baby shampoo on my mask and then rinse at the surface.
I'm guessing that masks will be more susceptible to fogging in colder waters where you have a bigger temperature difference than in warmer climbs. But that's just a guess on my part