After any initial recoil and turn, the element in question is fleeing away from whatever caused the panic. By continuing to run in the direction it is now heading, it may come into contact with friends it cannot pass through or by, or with impassable terrain *during its flee move*, or just move closer to enemy within 800 paces. It should try to avoid doing any of these if possible. To do so, it can make one turn of up to 90 degrees.
However, if that turn leaves it pointing at something similar to that which it is trying to avoid (any of friends it cannot pass through or by, impassable terrain or enemy) *and* that other is not more than 400 paces away *and* is also visible, then it does not get to turn at all, but instead must continue on its original course and suffer the consequences.
In short, in its panic, a fleeing element will look for a way to safety and avoid an obstacle in its immediate path, if all it sees are other obstacles, it just carries straight on regardless.
Note, while the '800 paces' bit is relevant, don't get too hung up on it at first - just get used to moving away from all enemy within the immediate vicinity and after a few flees, it will click how the 800 fits in.
Anthony