When you look at the structure of a six-membered ring, if all the molecular geometries around each atom in the ring are #\mathbf(sp^2)# -hybridized, then the whole molecule is planar.
It's not always the case for rings of other sizes, but for six-membered rings, it usually is true.
. But if all bonds have 100% restricted rotation AND are restricted to be in one plane, then it's even more likely that it IS planar (even if one atom appears to not be #sp^2# -hybridized. ). Pyrrole, furan, and thiophene are interesting five-membered ring examples of this.
With five-membered rings, having one heteroatom in addition to four #sp^2# -hybridized atoms helps, especially if that heteroatom has a smaller valency than carbon, but it doesn't guarantee anything.
Here are a few examples of planar and non-planar six-membered rings.
Notice how the ring gets more planar with an increasing number of #sp^2# carbons. That usually is how it goes, but not always.
For five-membered rings, it's harder to find examples, but here are some examples that may contain heteroatoms:
For rings with more than six members, it becomes less likely that all #sp^2# -hybridized ring atoms is the only requirement for planarity. Here's an example of a non-planar molecule with all seemingly- #sp^2# -hybridized carbons in its flat structure:
Its actual structure is:
So how would you actually know for sure? Do some X-ray crystallography or X-ray diffraction and determine the structure.