You'd think this was pretty straightforward. Punish someone if they make an illegal move and ensure they make a legal one instead. Sure, but things ain't quite that simple...
Let's consider the obvious illegal moves. You can't move your rook like a knight; you can't move your king into check; you can't castle through check. There are several other illegal moves like these. But what about some slightly rarer ones? Such as, castling after the king or rook has already moved; capturing a king; making two successive moves without allowing your opponent to play his move?
We'll look first at the basics. FIDE Rule 7.1 states that if the initial position of the pieces is found to be incorrect, eg the king and queen are found to be on the wrong sguares, the game must be restarted.
If the board is found to be the wrong way round (a Black square on the right) the game continues but is transferred to a correct board (Rule 7.2).
If an illegal move has been completed (i.e. the clock has been pressed), then the position immediately before the irregularity should be reinstated. If that cannot be determined, then the game continues from the last identifiable position, prior to the irregularity (Rule 7.4a).
For the first two illegal moves by a player, two minutes extra time are awarded to his opponent in each instance; for a third illegal move by the same player, the game is lost by this player. But the game would be drawn if the position is such that the opponent cannot checkmate by any possible series of legal moves (Rule 7.4b).
So that's the rules, but what actually happens if, for example, you try to castle but find you're either moving into check, or moving through check. In this situation you must move your king somewhere, as castling is fundamentally a king move. If it's impossible to move the king anywhere, then you can make any other move. But either way, your opponent will receive two extra minutes.
Suppose now that you don't notice that you are in check, and move a piece elsewhere on the board. If that piece is able to prevent the check, then it must be used accordingly. If that piece cannot prevent check, either by making a capture, or by blocking the check, then you are allowed to make another move to stop the check.
There's a false belief that if you make an illegal move during a quick-play finish, or when there is less than five minutes remaining on the clock, you lose the game. Not true. Your opponent just gets the two extra minutes. But in Blitz chess it is true (at any stage of the game), provided your opponent points out the illegal move before making his own.
If you capture the king, that is considered to be an illegal move. But be careful in Blitz chess, because if your opponent leaves his king in check (an illegal move), and you immediately capture it and claim the game, it is you that has made the last illegal move and therefore lose the game. If you had simply pointed out your opponent's error in leaving his king in check, then you would have won. By making your move you are deemed to have accepted his earlier illegal move.
If a game has been concluded, and during subsequent analysis it is found that an illegal move was played, then the result stands. If the illegal move had been pointed out before the game had finished (even if it was a number of moves earlier) then as stated above, the game is is reset back to the last identifiable position, ideally just before the illegal move was made. Under FIDE rules, there is no limit to the number of moves later that an illegal move can be pointed out, but in the US, where they have slightly different rules, there's a limit of 10 moves.
That's enough on this topic for the time being, but if anyone has any questions on illegal moves, or considers that any of the above statements is incorrect, please let me know!