When somebody casts a spell, it does not just happen right away. It goes onto the stack and chills there for a second. Then both players get a chance to be like, “Wait, I’m doing something too.” If someone casts an instant in response, that instant goes on top of the first spell. The card on top resolves first, so the last thing played is the first thing that actually happens. Kind of like cutting in line, but legally.