The explanation given within the films is that Neo (as the One) has a special connection to the Source which allows him to control machines in the real world. Neo asked the Oracle how he was able to stop Sentinels in the real world and she gave him the answer:
Neo: Tell me how I separated my mind from my body without jacking in. Tell me how I stopped four sentinels by thinking it. Tell me just what the hell is happening to me.
Oracle: The power of the One extends beyond this world. It reaches from here all the way back to where it came from.
Oracle: The Source. That's what you felt when you touched those Sentinels. But you weren't ready for it. You should be dead, but apparently you weren't ready for that, either.
The Matrix Revolutions
Neo was able to destroy the machines' weapons in The Matrix Revolutions in the same way. The films never explain exactly how Neo was able to control the machines in the real world, but it's worth noting that Neo was designed by the machines to carry machine code that enabled him to perform the function of the One. As explained by the Architect:
The function of the One is now to return to the Source, allowing a temporary dissemination of the code you carry, reinserting the prime program.
Your 5 predecessors were, by design, based on a similar predication - a contingent affirmation that was meant to create a profound attachment to the rest of your species, facilitating the function of the One.
The Matrix Reloaded
We know machine code carried within a human body can affect the real world because of Smith's control over Bane in the real world. Thus, the code carried by the One undoubtedly gave Neo a connection with the Source in the real world as well.
The biggest question is how Neo manages to communicate orders via the Source in the real world, and the films don't explain how. Presumably he communicates wirelessly using his head jack. Zion's use of EMPs should disable the head jack, but the EMPs apparently only disable head jacks temporarily: humans can't be connected to the Matrix during an EMP blast but they can re-enter the Matrix after the blast, and Smith/Bane is put in a coma by his EMP blast in The Matrix Reloaded but eventually regains consciousness. Machines like Sentinels are permanently disabled by EMPs, but evidently the human body acts as a "battery" to re-start the head jack.
As an aside, the Matrix-within-a-Matrix (MWAM) theory is an understandably popular fan theory which easily explains how Neo controlled the machines in the real world. However, it suffers from several notable inconsistencies with known facts. For example:
- Neo's powers in the real world are much more limited than his powers in the Matrix. In the Matrix he can fly, stop bullets, endure incredible acceleration, bring Trinity back from death, etc. But in the real world he can only affect machines, he can't fly (otherwise why would he need Niobe's ship to get to the Machine City?), he is easily and permanently injured (blinded) in his fight against Smith, and -- most notably -- he is unable to save Trinity from death. If the "real world" is just another Matrix there's no reason why Neo wouldn't be just as powerful in it.
- The Zion Archives depict the history of the Machine War and the creation of the Matrix. If MWAM is true then the machines would have deliberately provided the Zion Archives to Zion. But the story told in the Zion Archives portrays the machines in a very bad light and includes some very graphic scenes of human suffering caused by the machines and the seemingly pointless detonation of a nuclear weapon by the machines after the humans had already surrendered. If the machines deliberately provided the Archives to Zion, they would have omitted those scenes and portrayed a story in which it was all the humans' fault for pre-emptively attacking the Machine City and scorching the sky.
- In the final scene of The Matrix Revolutions, the Architect tells the Oracle that she played a "dangerous game" by breaking the cycles of the Ones and bringing about peace with Zion. But if MWAM is true, there was no danger nor purpose to the Oracle's plan -- the humans are no closer to escaping the outer Matrix of the "real world". And there would have been no reason for the Oracle and the Architect to have that conversation amongst themselves in the first place.
- The machines cooperate with Neo to defeat Smith and as a result end the war with Zion. They act as if they are really threatened by the loss of their one power source. But if the "real world" was another Matrix the machines wouldn't care if Smith took over the Matrix -- the humans in the "real world" would still be providing power for the machines in the "really real world" regardless of the status of the Matrix.
- Humans appear as green code in the Matrix but only the machines have gold code in the real world. If the "real world" was just another Matrix with gold instead of green code then humans and the surroundings should also look like gold code. What we see is exactly consistent with what we would expect if everything machine-generated appears as code and the real world is reality -- everything in the Matrix as appears as code but only the machines themselves appear as code in the real world.
- Scenes in the Matrix have a green tint but scenes in the real world do not.
Okay, but what if the "real world" is a Matrix controlled by different machines? In other words, what if the machines we see in the films were themselves products of Supermachines which control the Matrix of the "real world"? Presumably the machines in the films would just be another level of control for the Supermachines and the machines wouldn't be aware of the Supermachines; the machines would think that the "real world" was real and not just another Matrix. This would explain some things like why the machines acted threatened by the loss of their power source in the "real world", but there are still inconsistencies with this Supermachine theory. For example:
- The alleged Supermachines would have full control over the "real world"/"outer Matrix" and should be able to suppress Neo's ability to see the machines as gold code in the "real world". They apparently can prevent him from seeing anything else as gold code in the real world (e.g. other humans, who would still be simulations and thus appear as code in the "outer Matrix"). So why can't they prevent him from seeing the machines, too? Allowing Neo to see the machines as gold code would seriously undermine the usefulness of the "outer Matrix". The Supermachines wouldn't allow it, if they existed.
- Similarly, the alleged Supermachines should be able to prevent Neo from controlling Sentinels in the "real world". The Supermachines certainly have an incentive to do so since they would want to do everything possible to help their machines win the war and keep the humans bottled up in the "inner Matrix". Again, the fact that the Supermachines are apparently unable to do this despite having full control of the "outer Matrix" gives us reason to believe that they don't exist.
- As with the regular MWAM theory, the alleged Supermachines have no reason to provide the humans with the Zion Archives, which portray the machines brutally killing the surrendered humans. This only serves to fan the flames of hatred against the machines. From the perspective of the Supermachines it's preferable that, upon possible defeat of the machines, the humans enter into a truce with the machines as they did at the end of The Matrix Revolutions. That way the humans would just be content to live in the "real world", thinking that they'd already escaped the Matrix. Giving the humans access to the Zion Archives just makes a truce between the humans and machines less likely.
The Supermachine MWAM theory is a complicated theory which assumes that there is another race of machines controlling another Matrix (and possibly more) which is completely unknown to both the humans and the machines. In contrast, the dialogue from the films suggests that Neo communicates wirelessly with the machines in the real world, and only a few minor assumptions are required to explain how that's possible: that Neo's head jack is able to communicate wirelessly with the machines in the real world, and that the head jack can be rebooted after an EMP by its human "battery". By Occam's Razor, the MWAM theory is less likely to be true.
Both versions of the MWAM theory also suffer from another, out-of-universe problem: if MWAM is true then all the events in the entire Matrix franchise are ultimately meaningless. The story starts with the humans imprisoned in multiple Matrix levels, a lot of things happen, and the story ends with the humans imprisoned in multiple Matrix levels.