To beat a Cannon Rush
I've fended off cannon rushes a few times as Zerg (well every time Its happened to me since the game was released actually

)
I sometimes stuff up after I have defended the cannon rush, but basically I can always come out of the cannon rush as Zerg ahead (effectively its my game to lose)
- Scout your base especially if you have not seen a probe yet or you see the probe and it goes off into the fog
- If the probe warps in a pylon pull 3 workers off the line and attack the probe. Try to prevent it from getting "In Range" of the pylon. Make sure you kill the probe* (when probe is dead tell the 3 workers to kill the pylon)
- If the probe warps in a forge, pull 4-5 workers off the line and kill the forge. If it warps in a cannon** pull 3 workers off the line and kill the cannon (do this per cannon) Send any workers not engaged in killing something back to the mineral line
- Keep pumping out workers do not stop doing this
- Keep to your build order as best as possible (make sure you can produce infantry)
- As soon as you have infantry use them to kill off any remaining; probes/cannons/pylons/forges/warpgates (in that order)
- Build up a fast army (reapers/lings/lotts/stalkers) and attack the enemies mineral lines (ignore everything but probes).
- Profit
Once your base is clear, you have a reasonable sized army (300-500 minerals worth) you should be trying to attack their mineral line. (if you kill off all their probes at this stage it is game over)
Some protoss will basically wall in and cannon up. If they do this do not sacrifice your units in an attempt to reach the mineral line. Just park your units outside their base and prevent them from expanding, whilst you expand yourself and tech up to units that can bust down the blockade (or circumvent it all together)
Zerg - Mass Mass roaches + Hydras / Broodlords / Ultras
Terran Siege tanks + MMM Transition into mass Battle Cruisers
Toss Immortals/Colossus + Zealot+stalker. Transition into mass Carriers
If you are going tier 3 make sure you have and army build that can beat mass stalkers/ mass zealots and can deal with a VR /Mothership/Carrier rush. As these are what turtle Protoss tend to transition into. Make sure you scout (even if it means sacrificing a unit or 2) There base to see what they are doing.
The aim is when dealing with a contained turtling enemy is to
A) Ensure they can't expand
B) Ensure they can't get any units (by land, air or proxy building) out of their base
C) To ensure you can expand at will and amass a massive army and ability to rebuild your army at rapid pace (and to build the units you need to counter whatever units they have)
Once you are in this position then it is about time to knock down their door. You should should be able to tell roughly when they have mined out, based upon the time you have mined out your main. They will almost certainly have a mineral reserve so there is no need to rush in, just chip away at the defenses, keep your losses to a minimum but make sure they lose stuff every attack. Re-enforce your units as and when you drop below 200/200
In terms of ensuring they do not slip out of their base. You need to ensure you have complete vision of the perimeter of their base as well as observation of all the expansions on the map.
As Zerg - Use overlords + Overseers + Creep
As Toss - Use Pylons + observers
As Terran Use Radar Towers + Turrets
As Zerg - put a swarm of overlords around the perimeter of their base so that you have
*Remember that when a worker is surrounded the player can tell that worker to mine some minerals and it will "glide" past any unit obstacles. A good Protoss player will know this so if you do surround the probe. It may just slip past your units and keep running around dropping pylons and cannons everywhere. So just keep up the chase until you kill it.
** Kill the cannons first because A) they have less life; B) they cost more (each cannon is the cost of a gateway.); C) Cannons hurt, Pylons don't