This is a really common problem. When there are fewer pieces on the board, the position looks simple, and your brain relaxes right when you need maximum concentration. Plus, endgames require completely different skills - what matters there is the king, pawns, tempos, the square rule. It's easy to lose focus if you've been playing actively and aggressively the entire middlegame.
Start by just learning the basic endgames. King and pawn versus king, simple rook endgames, pawn endings - this isn't boring theory, it's a toolkit that saves you from making mistakes out of nowhere. When you know that king plus pawn should be winning in a position, you won't start making thoughtless moves out of doubt. Solve endgame puzzles - there are tons of apps and problem collections out there, takes 15-20 minutes a day.
Second is the psychological side. If you won the middlegame and feel like you're starting to rush or relax, take a pause before the endgame. Literally acknowledge it: okay, everything changes now, this needs a different strategy. Switch to calculating each move slowly, don't rely on intuition. People often lose a win simply because they start playing on autopilot. Even in a winning position you need to stay serious.