In one codimension one can be convinced of this quite easily. Consider the tracefree curvature tensor $ A^o = A - \frac{1}{n}Hg, $ where $g$ is the metric, $A$ the second fundamental form, and $H$ the mean curvature. For surfaces, the norm squared of $A^o$ satisfies: $ |A^o|^2 = (k_1 - k_2)^2, $ where $k_1$ and $k_2$ are the principal curvatures. On the other hand, $ 2|A^o|^2 = 2|A|^2 - H^2 = -2(H^2 - |A|^2) + H^2 = -4K + H^2 . $ So if $K$ and $H$ are constant, then the norm of the tracefree curvature tensor is also constant. Further, the symmetry of $(\nabla A)$ implies that every component of $A^o$ is constant, and so the principal curvatures are themselves constant functions. If their difference is zero, then the principal curvatures are equal and it follows that the surface is a sphere or a plane. In higher codimensions this is more complicated. If their difference is not zero, this contradicts the compactness of the surface.