how does this gradient invert the alpha mask? and how does it work?

That is what is called a “dependant texture read”. That is when one texture is used to look up the texture coordinates of another texture. The approach is extremely slow and should be used sparingly. In this case it is inverting because the texture is inverted from what the texture in the coordinates is. When the coordinate 0 on Y, this sees white, and sees black when Y = 1.