How does a mezuzah work? How can such a tiny device, with no electronic parts, protect an entire household of people wherever they are and whatever they are doing (certain high risk activities may be excluded)?

True, it's not the mezuzah that provides the protection, it's the Master of the Universe Himself (you can find His name written on the backside of every mezuzah: Shin Daled Yud). He says, "Put up a mezuzah on your doorway and I'll protect you when you're inside and out." But still, why a rolled up parchment device? And why on the doorway? And why any device at all—couldn't He just say, "Trust Me, and I'll take care of you"?

To answer this question, we have to first escape our nanobrain, subjective boxes and view matters from a cosmic perspective. The entire cosmos is His masterpiece of architecture. But He doesn't really live within it until there's a mezuzah on the door. The mezuzah is something like the artist's autograph, so that anyone looking will know who did this.

The problem is, a whole lot of critters in this universe don't want to recognize Who did this. They don't want to recognize that anyone did this at all. They just want to be left alone in the universe as though it were always here and they can do whatever they like with it. Those are the critters you gotta watch out from.

So we put up a mezuzah, effectively signing His autograph onto His masterpiece. It needs to be written by a scribe with an awe of heaven and care to the finest details of the Divine law—so that it corresponds in detail to the Grand Autograph Above. What is that Grand Autograph? The original words of thought by which you were conceived: The story of a being called Israel who loves G‑d with all its heart, speaks words of Torah wherever it goes and translates that Torah into mitzvahs. Every nuance of every letter in the mezuzah has something to do with your life down here.

It goes on the doorway, because doorways are the interface between one world and the next—which cosmically speaking would be the sefira of Malchut (Dominion), a.k.a. "The Shechina" which is all about His presence in this world. By doing so, our homes are transformed into scaled down versions of His own cosmic home (the universe). The world we live in becomes effectively a different kind of world—one in which those nasty critters don't belong. But you do.

For some heavy-duty Kabbalah on the mezuzah, see Doorways and Shady Characters - The Zohar.