You commit to the initiative while your own king is exposed, and the counter-attack arrives first.
Your king was caught in the open — uncastled, its pawn shield cracked, or its defenders pulled away — and the attack crashed through. The king isn’t a fighting piece in the middlegame; it’s the thing you protect. Castle early, keep the three pawns in front of it intact, and leave a piece or two nearby. A safe king lets the rest of your army attack without fear.
The good news: it is a pattern, not bad luck — and patterns can be trained. Step one is measuring yours: how often it appears in your real games and how many rating points it drains. Then the daily repair: 5 minutes on your own positions, not invented puzzles.