Which Source of Energy Comes First?
To expand on a comment by user253751, it has to work because of Kirchhoff's current law.
There will be a point in the system where the grid supply, the solar input and the house load all meet.  That could be in a breaker box, or some other sort of junction box.
Kirchhoff's current law says that the total current going into a point must equal the total current going out.  If it were not so, electrons would be piling up at that point.
So imagine it's a sunny day.  The solar inverter is pushing out 10A.  The house is drawing 6A.  That means 4A must be going out to the grid.
Suppose you turn on a high power appliance, and the load increases to 16A.  The solar is still generating 10A, so 6A must be coming in from the grid.
So nothing &quot;decides&quot; where the power comes from.  It's all done by the laws of physics.

I answer to the comment below, solar inverters are clever computerised things.  The constantly monitor the output of the solar panels, and suck out as much power as they can, given how sunny it is.  They then adjust the voltage and phase of the AC that they generate so that they are always pushing out as much power as they can.
So as the grid voltage fluctuates slightly throughout the day, the inverter is adjusting its own output to match.