The inverter can sniff the grid power at frequent intervals to determine its voltage and phase.

It outputs at the same phase and frequency as the grid, and with a slightly higher voltage (think of a ball rolling down a slope from a higher voltage to lower voltage).

If the grid is down then it stops the inverter for a minimum safety period (some minutes iirc)