For a molecule with  atoms, the positions of all  nuclei depend on a total of 3 coordinates, so that the molecule has 3 degrees of freedom including translation, rotation and vibration. Translation corresponds to movement of the center of mass whose position can be described by 3 cartesian coordinates.
A nonlinear molecule can rotate about any of three mutually perpendicular axes and therefore has 3 rotational degrees of freedom. For a linear molecule, rotation about the molecular axis does not involve movement of any atomic nucleus, so there are only 2 rotational degrees of freedom which can vary the atomic coordinates.
An equivalent argument is that the rotation of a linear molecule changes the direction of the molecular axis in space, which can be described by 2 coordinates corresponding to latitude and longitude. For a nonlinear molecule, the direction of one axis is described by these two coordinates, and the orientation of the molecule about this axis provides a third rotational coordinate.
The number of vibrational modes is therefore 3 minus the number of translational and rotational degrees of freedom, or 3–5 for linear and 3–6 for nonlinear molecules.