So, boiling it down, attacks of opportunity require two things-- that you are within range, and that you trigger it. Generally, this means that you have to be in an adjacent square, and take one of the actions which prompt attacks of opportunity. However, creatures with reach, or creatures using weapons with reach, can make attacks of opportunity against targets that are two squares away (10 ft.).
What then provokes an attack of opportunity?
1. Moving out of a threatened square; and
2. Taking certain actions within a threatened square.
The actions which, of course, most often provoke an attack of opportunity are making a ranged attack and casting a spell. Casting a quickened spell, and casting on the defensive are exceptions to this rule. Unarmed attacks (in most instances) will also trigger an attack of opportunity. Drinking a potion, reading a scroll, and stabilizing a dying character will also do so. If you aid another in an action that would normally provoke an attack of opportunity, the act of aiding will also provoke one.
Using spell like abilities provokes attacks of opportunity, but channeling energy does not. Various move actions also provoke, including loading a hand or light crossbow, picking up an item, sheathing a weapon, and retrieving a stored item.