But how do they do it technically? Some countries require anti-islanding devices for grid connected solar, these are around $1000. That alone is much more expensive than the $300 plug in panels.
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)