Unlike en passant, this one comes up often. Practically every game one or the other of the players castles or has a chance to castle. Like en passant, castling is a very restricted move. Certain conditions have to be filled in order to be able to castle.
King and Rook
Before we get into the special conditions, the basic castling move consists of moving both a king and a rook on the same turn. It is the only time in chess when you can move two friendly pieces on the same turn. The king moves two squares along his home rank, while the rook jumps over the king and lands on the opposite side he started from. Both moves are impossible separately, since kings normally move only one square in any direction while rooks normally don't jump over anything, needing a clear file or rank in order to move at all.
White's king and h1-rook have not moved. There is nothing in between the two except empty squares.
White has moved the king two squares to the right along the first rank and jumped the rook over the king to its other side. Castling is completed.
Black's king and h8-rook have not moved. There is nothing in between the two except empty squares.
Black has moved the king two squares to the right along the eighth rank and jumped the rook over the king to its other side. Castling is completed.
Castling serves two purposes. One is to tuck the vulnerable king away in a corner for safety during the early part of the game. Opposing pieces may be flying around the middle of the board, and staying there may not be healthy for the valuable king. The other is to allow the rook to get involved in the action through the middle of the board. This is usually much better than moving the rook up the h- or a-file along the edge of the board.
In order for castling to be possible, the following conditions have to be present:
The squares between king and rook must be empty.
Both king and rook must be on their original square.
Both king and rook must be unmoved.
The king cannot castle into check.
The king cannot castle out of check. Remember, the three ways out of check did not include castling.
The king cannot castle through check.
Castling is the newest move in chess. It has been standardized only within the last 150 years. Before that there were many ways to castle, including the king and rook actually trading places or using it as two separate moves, with the king moving first and the rook jumping over him the next move.
That last restriction is a little tricky. It means that the king can't pass over any square that is directly under control of an enemy piece. For example, a White king on el and a White rook on h1 are ready to castle, as long as neither has moved and the king is not in check and the f1-and g1-squares are empty. But if there is a Black bishop on d3, which controls the f1-square, castling is not possible.
White cannot castle because the king would wind up on g1, in check from the b6-bishop.
White cannot castle because the king is in check from the Black queen along the e-file.
White cannot castle because the f1 -square he must pass over is controlled by the d3-bishop.
Kingside and Queenside
There are two types of castling, depending on which direction you decide to castle. In kingside castling the White king goes to g1 while the White h1-rook goes to f1. (The Black king goes to g8 while the Black h8-rook goes to f8.) The kingside is simply the half of the board where the kings start out. Thus kingside refers to the e-, f-, g-, and h-files.
In queenside castling, the king goes in the other direction. The queenside is that half of the board where the queens start out. Thus queenside refers to the d-, c-, b-, and a-files. The White king goes to c1 and the White rook on a1 goes to d1. (The Black king goes to c8 while the Black rook on a8 goes to d8.)
Black's king and a8-rook have not moved. There is nothing in between the two except empty squares.
Black has moved the king two squares to the left along the eighth rank and jumped the rook over the king to its other side. Queenside castling is completed.
The restrictions about castling out of, into, and through check apply only to the king. If your rook is under attack from an enemy piece but your king is safe on its starting square, the square it passes over in the process of castling, and the square it lands on, castling is legal.
White's king and rook have not moved and there is nothing but empty squares between the two.
White has completed castling. The rook was under attack, but that's not relevant to castling.
The enemy can also cover the extra square the rook passes over in queenside castling during castling.
The White rook covers the b8-square, but this is not relevant when castling comes into play.
Black has successfully castled. The b8-square doesn't involve the king, so its situation is not relevant.
You will notice that in queenside castling there is an extra file to take into account. The king always moves two squares to the side when castling, so it is the rook that has to travel farther in queenside castling.
A particularly important restriction to castling is that it can only be played once in a game by either player. This is inherent in the castling rules, since castling cannot take place unless the king and the rook he castles with are unmoved. Thus once you castle, that condition can no longer be met for the rest of the game.