Castling can be performed on either the kingside (short castling, notated O-O) or the queenside (long castling, notated O-O-O). To castle legally, neither the king nor the chosen rook may have previously moved, no pieces can stand between them, and the king cannot be in check, pass through check, or land in check.
Castling is almost always a good idea in the opening or early middlegame because it tucks the king behind a wall of pawns and brings a rook toward the center where it can be more active. Delaying castling can leave the king vulnerable to tactics, especially when the center is open.
Grandmasters occasionally delay or forgo castling when the position is closed or when an attack on the opposite wing is more urgent, but for most players castling early is a reliable habit that leads to safer, more coordinated positions.