How does logic gates know if electricity is flowing?
This question is similar to asking "How does an apple know that a gravitational pull exists and that it shall draw to it?"

The basic answer is: The physics says so.

This is similar to your logic gate example. A logic "not" gate is just a symbolisation of reality. Scientists built a semiconductor device that had the "inversion of signal" behaviour and called it the "not gate". It's not the other way around.

Why there exists a material that can invert a signal? Well, technically, we don't know the "real" reason. We just know the laws of nature, deducted from experiments. 

For example: the laws of physics says that a combination of these:


CMOS Two-input NOR Gate:
Source

Will act as a NOR gate which can be used to construct greater logic. How does a nor gate know about electricity? Well, it lies on the intricacies of P-N juntions and their combinations. Source

The brief explanation of P-N junctions can be summarized as: "the electron give/take probability of (chemically) different materials."

So basically, you engineer a bunch of P-N structures and they act somehting like what mathematicians call a logical NOR.

By the way, I belive, NOR was a universal gate. So, you can create other logic gates from NOR. (That includes the NOT gate)