Many things in Starcraft come down to timing and awareness.
It sounds like a no-brainer that an unaware enemy will be caught off-guard by cloaked units. But the heart of the concept is that your opponent cannot prepare for everything.
But if you combine it with timing, it is even more powerful. Many strategies in starcraft rely on timing. A 6-pool is a very cheap strategy to inflict early harrassment on an unaware player. It can be retaliated, but it requires awareness. The same goes for a terran-drop. Or for a cannon rush.
So, you might say that you can simply scout everywhere and prepare to build all necessary counter units. But that is not possible in a fast-paced starcraft game. You need to evaluate the cost of every unit.
Cloak-Scenario: Building a detection unit/building is expensive. A rocket turret or science vessel is something that comes very late in the techtree. And energy of your orbital command is much better used in mules.
And even if the terran decides to build a turret, the turret can only be in one place. If he builds a science vessel, it can only be in one place, and even the scan will only persist for a few seconds. So in addition to having the need to be aware of your strategy and to be able to have the correct counter unit out at that very time, your opponent has not beaten your strategy yet. You can simply dodge his detection, split your forces and therefore force him to commit even more resources to fend off your strategy. And since you only need very few dark templars for that, you can freely build up an entirely different army to transition to the next timing move.
The same applies for Protos: Canons might not be part of most players build plan and observers are fairly late in the tech-tree.
And for Zerg: They will need to upgrade their Overlords, after which they will be able to detect you, but only in one place at a time and move slowly. Sporecrawlers might also not be present yet.
So in short: inflict (timing) pressure on your opponent by perfecting your build order. And things will get alot easier.