The usual rule is that you try to reset the position to the one immediately before the illegal moves were made (in this case, everything after the check). Failing that, reset to an even earlier position. Failing that, then you should find an arbiter to decide what to do. In blitz games, illegal moves if noticed immediately can cost you the game, but only if there is no arbiter and if the opponent has sufficient material to win.
The current laws of chess seem to be a lot less harsh on illegal moves than used to be the case, when the player who'd made the illegal move was often obliged to move his king instead if possible; but the old rules say for the situation you describe that:
"If the King has been in check for several moves, and it cannot be ascertained how it occurred, the player whose King is in check must retract his last move and free his King from the check; but if the moves made subsequent to the check be known, they must be retracted."
While these rules are older, they are essentially the same as the modern ones and clearer on the case in point. No penalty; just retract to the last known legal position.