In simple terms (handily enough the only terms I understand), counterfactual thinking occurs when we imagine how things might have turned out. When something happens — especially something significant — we think about alternatives to our current reality in order to place that event in context.
Counterfactual thinking sometimes makes us feel good about where we are in comparison to where we could be.