Dealing with the dreaded Terran Base Race
It sounds like you're building a large army and using it to gain map control, but then are falling prey to things like drops and backdooring.
If you have map control your first thought should never be: Crush my opponent, as you're essentially sacrificing an advantage (map control) for an unsure victory (because he doesn't control the map doesn't mean he can't take you). Instead your thought should be: take the map. I think you'll find this change in philosophy will lead to situations where you have a much more powerful economy than him and you're able to kill him with attrition.
Now if you're falling prey to things like BD here is a quick check list for you:
- Are you scouting? an Observer should be the first thing you build out of Robo. If you don't go Robo, a Hallucination works just as well. Sometimes scouting means sacrificing some Zealots in a pointless charge to just get inside his base.
- If you're scouting, are you seeing it coming? Overlords on the edge of your base, Starport with Reactor + 3 Rax, all of these are indicative of certain strategies you should watch out for.
- If you see it coming, how are you responding? If you have map control that means you should be able to respond quickly. Shutting down these back door attempts is often a bigger victory than a straight up conflict
- Finally, Do you have map control? If you don't have map control that means he does, and if he's beating you with map control then your problem is not "how do I deal with a base trade?" but rather, how do I regain the advantage.
Scouting is key. Knowing what your opponent has and what he is doing with it is crucial to victory. I would focus on dealing with his main army. If you can keep them bottled up in their base, many players go air. If you know that their main force left their base, try to slip a few zealots in to kill workers. Use your main force to kill their force. If you have enough for Templars and Psi Storm, they work well with weakening marines.
The key here, like @tzenes said, is map control. Fighting through a natural choke point is a terrible way to go. Try to take on the terrans out in the open or by a cliff that you can station your Stalkers on. As long as you can take out their air units, you can use your zealots to hold them on the "killing field" without loosing your ranged attacks.
I sometimes station a random pylon with two cannons near the base, but out of the way to warp in a few units to attack when the main force leaves. They can either turn back, or split their forces to deal with that small force. It can be tough to micro that, but you'd be surprised what a few zealots can do when no one is looking. If you can get them to look away from the main fight, it would be worth it.
Hope this helps