Castliing protects the King not the Queen. Can go either side provide clear spaces between rooks and king. Rook moves center, King to other side of rook.
But the castled King cannot pass through a check in the process of moving to his castled location, otherwise the move is not allowed.