All about Immediate Actions
An immediate action, according to the SRD, is used as follows
Much like a swift action, an immediate action consumes a very small amount of time, but represents a larger expenditure of effort and energy than a free action. However, unlike a swift action, an immediate action can be performed at any time — even if it's not your turn. Casting feather fall is an immediate action, since the spell can be cast at any time.
Using an immediate action on your turn is the same as using a swift action, and counts as your swift action for that turn. You cannot use another immediate action or a swift action until after your next turn if you have used an immediate action when it is not currently your turn (effectively, using an immediate action before your turn is equivalent to using your swift action for the coming turn). You also cannot use an immediate action if you are flat-footed.
And that's pretty much it.
An immediate action isn't like taking a standard action to ready. An immediate action can't be taken in response to another event then occur before the event it's responding to. There's no last-in,-first-out in Dungeons and Dragons 3.5.
Further, while some events are viewed as having discrete steps at the gaming table, some of those events don't actually have all those steps according to the game, and therefore must be resolved before immediate actions can be taken.
Unlike many trading card games, there's often no specific declare phases or discrete steps (e.g. a creature doesn't pick a target then make an attack roll then roll damage, allowing responses to each). A creature does something that needs to be resolved (e.g. make an attack, cast a spell) before something else can occur. Immediate actions, then, can't be taken while the other creature has an event unresolved, but an immediate action can be taken between events.
The DM must determine when an event has multiple steps that can be interrupted (during which creatures can take immediate actions) and when an event is finally resolved, but the game provides some guidance.
Event: Attack Roll
An attack roll
represents your attempt to strike your opponent on your turn in a round. When you make an attack roll, you roll a d20 and add your attack bonus. (Other modifiers may also apply to this roll.) If your result equals or beats the target’s Armor Class, you hit and deal damage.
Thus once the attack roll is made, an immediate action can't taken until damage is dealt. The attack roll, once made, determines both whether an attack hit and inflicts damage. Note that this differs from Ernir's answer to this question.
Event: Saving Throw
A saving throw is resolved discretely. There are opportunities for decisions during saving throws. For example, "A creature can voluntarily forego a saving throw and willingly accept a spell’s result." This opportunity for decision-making permits immediate actions to be taken when rolling a saving throw (e.g. the psionic power evade burst)
Some special abilities interrupt events that can't otherwise be interrupted. For example, the elan's supernatural ability resilience says, "As an immediate action, an elan can reduce the damage he is about to take by 2 points for every 1 power point spent." If the elan weren't about to take damage from an attack, he couldn't use this ability, so this ability allows the elan to take an immediate action before damage is inflicted during an attack.
A monster makes an attack roll versus a conjurer with the supernatural ability abrupt jaunt alternative class feature and who has prepared several spells with immediate action casting times.
- The conjurer can't take an immediate action after the attack roll's made but before the conjurer knows if it landed.
- The conjurer can't take an immediate action after the attack roll's made but before damage is dealt.
- The conjurer can take an immediate action after a creature's attack roll has resolved and before me makes another attack roll when the creature makes multiple attack rolls during a full attack action.
- The DM must determine if the conjurer can take an immediate action between the movement and the attack of a charge. (PH 154 says, "Charging is a special full-round action that allows you to move... and attack during the action.") Reminder: It's already really tough out there for melee characters.
Consider this text from the 2nd-level Clr spell close wounds [conj] (SpC 48), which has a casting time of 1 immediate action,
If you cast this spell immediately after the subject takes damage, it effectively prevents the damage. It would keep alive someone who had just dropped to –10 hit points, for example, leaving the character at negative hit points but stable.
The game must make a special case for the spell close wounds because otherwise the spell wouldn't do what the game wanted it to do--keep folks alive. Without that text, the damage would resolve, the creature would die, and the spell close wounds wouldn't be anything more than a weak, off-turn cure spell.