I had a little conversation one of my football friends and we had this interesting situation come up during the conversation.

As I understand the offside rule, the offending player should not be closer to the goal than the second-to-last defender.

But, what if at the point of the player receiving the ball, one of the defenders go out of play? Like for example, the goal keeper or the other defender is injured or goes out of the field. Would it still be considered an offside because there is only a single defender between the offending player and the goal? If yes, why can't this rule be exploited to effectively nullify the goal scored, if any?