Flanking
To flank a foe, you and your ally must be on opposite sides of the creature. A line drawn between the center of your space and the center of your ally's space must pass through opposite sides or opposite corners of the foe's space. Additionally, both you and the ally have to be able to act, you must be wielding melee weapons or be able to make an unarmed attack, you can't be under any effects that prevent you from attacking, and you must both have the enemy within reach. If you're wielding a reach weapon, you use your reach with that weapon for this purpose.
3D Flanking
In these cases, it's usually best to have the GM make the call on who's flanking rather than trying to do meticulous measurements in three dimensions.