Making a correct offside (gaining advantage) decision can be tricky. As referees, we may forget the fact that a player who receives a ball was in an offside position first. But we may believe they were on just because they were onside when a deflection or a save happened.
If a defender makes a deliberate play on the ball, offside is reset. But when there is a deflection or a save, it is not reset. Please see the video below. At the moment of the first shot, there was an offside player. The ball is deflected and saved before the offside player scores. This goal cannot stand. Because the defender did not deliberately play the ball (there only were a deflection and a save), the player who seemingly scored the goal must be penalized for offside (gaining advantage).