@Hope4Now is right.
We humans share this adaptive trait with many, many (most? all?) other species. When threatened, humans have the option to fight, flee (flight), or dissociate. (There's a fourth I think but damned if I can remember it.) When fighting or flying (fleeing, running away) won't work, either because the threat is too powerful, too fast, or both, dissociation is a way to shut down certain brain areas and become sort of dumbstruck or spaced out. By reaching that state, you become no threat and no flight risk to an attacker and so increase your odds of living through the situation so that you can one day contribute to the gene pool.
While dissociated, certain brain areas reduce function if not shut off altogether. These functions turn out to be those that record normal events as memories you can recall later. Simultaneously, the brain areas that control fear are running at full tilt, and they don't really record data but sort of retain the fear state. Now this is a super gross oversimplification of something that's really pretty complex. There's quite a bit of research going on in this area.
In a good scenario, you live and get a chance to socialize with loved ones, process the event, be comforted, and so on. That way, what happens gets integrated as normal memory, the state of fear relaxes, and you go on with life. Where this whole system doesn't work so well is when you don't receive comfort, you don't process the event as normal memory. Your body sort of stores the fear and you become hypervigilant for anything that reminds you of the traumatic event. And when you sense a reminder, you get triggered and once again, dissociate as if you're re-experiencing.
Don't worry too much about not fully understanding dissociation. It's very complex and is after all about the human brain. We know more about outer space than the earth's deep oceans, and we know more about the deep oceans than we do about the human brain.