You create a flank any time you have maneuvered on the battlefield in such a way as to inconvenience your opponent due to the threatening actions of yourself and your allies. Flanking is the most common way in which teamwork with your friends provides you with a tangible in-game benefit.
If you have a flank against an enemy creature, you gain a +2 flanking bonus to your attack rolls against the creature. The same is true for your ally who is also flanking the creature.
Flanking only counts for melee attacks within your threatened area. If you do not threaten, you cannot flank or gain benefits from a flank. In order to threaten, you must be proficiently wielding a weapon capable of doing lethal damage in one or more of your hands, and able to perceive the target in some way. Some classes, feats, and racial abilities may present other ways to threaten squares.
To determine whether you have a flank, both you and an ally must be threatening the same creature. Trace a line from the center of a square you occupy to the center of a square your ally occupies, and if that line passes through opposite sides or opposite corners of the enemy's space, you have a flank against that enemy.
Corner squares ONLY flank with the opposite corner squares. Thus, a line which passes through a side of the creature's space and exits out a corner of its space (or passes through a corner of the creature's space and exits out a side of its space) is not a flank.
If your line to determine a flank enters the side of a target creature's space and exits an adjacent side (i.e. through the south edge of the creature's space, and out the east or west edge of its space) it is not a flank. Also, any line which passes directly along the border of a target creature's space is not a flank.
However, when determining a flank against a size-large or larger creature, you only have to be able to trace the line against any portion of the opposite side of its space. This means that, against larger creatures, it is usually easier to establish a flank against its side squares than its corner squares.
Only allies which threaten the creature you are threatening can provide flanks. NPC's who are not allies, or allies who are not threatening the creature in question, cannot contribute to a flank. Creatures not wielding a weapon which threatens, or that have a reach of 0 can't flank an opponent because they don't threaten adjacent squares. (Such creatures only threaten squares in their own space.)
If a creature is size-large or larger, it can choose which of the squares in its space it wishes to determine flanks from (i.e. it does not draw its line from the center of its total space, but from the center of any one square within its space). Thus, larger creatures often have an easier time flanking smaller creatures.
If a single square contains more than one creature, and you establish a legal flank to that square, you flank all creatures in that square.
Reach and Flanking
Creatures with reach (or reach weapons) don't have to be adjacent to a creature they're attempting to flank.
In addition, when determining whether the flank a creature or not, they can trace a line from any corner of their own space to any corner of an ally's space who is also threatening the target creature. If this line passes through opposite sides (or opposite corners) of the creature they are attempting to flank, it a successful flank. As with normal flanks, a line that only passes through adjacent sides of a target creature's space, or only passes directly along a target creature's border, does not grant a flank.
Note that size large or larger creatures with reach can start this line from any corner of any square they occupy.
In cases where there are very large size differences between foes on the field (i.e. 2 size categories or more), it is possible for smaller foes to be inside the space of a larger figure. In such cases, then neither figure is considered to be Squeezing.
This often occurs because the larger creature makes use of the Stepover feature to move over top of, stop in the space of, or move past a sufficiently small creature.
For a smaller creature to enter a larger creature's space requires the Clamber feature of Might. Creatures wishing to pass all the way through another creature's space must use the Overrun maneuver. Simply avoiding attacks of opportunity while moving through threatened squares requires the Tumbling feature of Acrobatics.
While inside the space of a creature two size categories larger than itself (or more), a smaller creature can gain internal flanking against the larger creature if he has an ally that threatens the larger creature from any space.
The larger creature flanks the smaller (internal) creature if any of the larger creature's allies are able to threaten the square the smaller creature occupies. The larger creature's ally also gets a flank against the smaller (internal) creature, in this case.
ALL of any creature's occupied squares are considered to be part of its threatened squares.