It is all about minimizing the energy of a molecule.
In the case of carbon, the only molecule that adopts a perfect hexagonal geometry in its ground state is benzene (and its derivatives that possess a 6-fold rotational axis). In this case the hexagonal geometry is adopted because all of the carbons are $\ce{sp^2}$ hybridized. The ideal geometry (lowest energy) for an $\ce{sp^2}$ hybridized carbon involves three 120 degree bond angles around the carbon atom. This can be accomplished in the case of benzene by placing 6 carbon atoms in a plane and connecting them as shown to produce the benzene structure.

Other carbon-based molecules may project as hexagons, but they are not hexagons when viewed in 3 dimensions. For example, cyclohexane is not flat like benzene, but rather typically exists in what is called a "chair conformation" in its ground state where the C-C-C angle is around 109.5 degrees.

why not octagon?
Some other regular polygon shapes can be found in molecules. For example, the cyclopentadiene anion exists as a regular pentagon, and the cyclooctatetraene dianion exists as a regular octagon. Both of these molecules contain only $\ce{sp^2}$ hybridized carbons.

As mentioned above, these atoms prefer to exist in a geometry with 3 equivalent 120 degree bond angles. However, the internal angle in a regular pentagon and a regular octagon are 108 and 135 degrees respectively. Since these angles deviate from the 120 degree ideal, these molecules will be strained, unlike benzene.
What other ring shapes are preferred, low-energy minima?
We've already discussed the $\ce{sp^2}$ case where the preferred 120 degree angle is found in benzene which exists as a hexagonal structure. $\ce{sp}$ hybridized carbon is most stable when the X-C($\ce{sp}$)-X bond angle is 180 degrees. Clearly small or medium size rings comprised solely of $\ce{sp}$ hybridized carbon cannot exist as stable entities. In the case of $\ce{sp^3}$ hybridized carbon, the closer we get to the ideal tetrahedral angle (an angle whose cosine = -1/3, ca. 109.47 degrees) the lower the energy of the molecule. We've already mentioned cyclohexane and while it's C-C-C angle is close to the ideal tetrahedral angle, it is not exactly the tetrahedral angle (because each of its carbon atoms does not exist in a perfect tetrahedral environment). If we fuse a few more cyclohexane rings onto cyclohexane, we can produce the molecule adamantane with 4 cyclohexane faces all in the chair geometry.

There is less strain energy in adamantane (per carbon) than in cyclohexane. The geometry at each carbon atom is closer to the ideal tetrahedral geometry, but still not quite there. How can we further elaborate adamantane into a ring system where all carbons have the ideal tetrahedral angle? Does adamantane look vaguely familiar? By continuing to add more and more cyclohexane rings we build the following ring structure...

... Diamond, where all of the $\ce{sp^3}$ carbon atoms are in the ideal tetrahedral environment, an extremely stable arrangement.
EDIT: response to OPs comment
So, the lesser energy state must be the reason that after six number
of atoms, five is the common one, right?
Kinda. Here's a table showing estimated ring strains for saturated hydrocarbon rings of various sizes.
[Image source Also, see this link for a discussion about why the ring strain for cycloheptadecane (-3.4 Kcal/mol) is suspect and probably should be higher.]
5- and 7-membered saturated hydrocarbon rings have similar strain energies, yet 5-membered rings are more common. We must consider the free energy of the process, enthalpy and entropy changes. In addition to strain (an enthalpic factor) we must also consider entropy. In ring forming reactions it is entropically more difficult for a 7-membered chain to bring both ends together for closure to a ring, then it would be for a smaller 5-membered chain.