Crystals have inspired a great many chemists because they are fascinating for a good reason. Not only are they aesthetically pleasing, but they serve as an excellent subject to tour a variety of theoretical subjects important for understanding high-level chemistry.
Crystalline materials are made up of periodic structures. We’re only going to primarily focus on binary compounds where there is not a high degree of covalency. There are several ways to think about this problem, but let’s start with the melting of a crystal.
We say that at some definite temperature a highly ordered crystal will melt into a liquid. Those of us familiar with the language of equilibrium thermodynamics might recognize that the change in free energy for this phase change can be written, at constant temperature, as,
$$ G_\text{liquid} - G_\text{crystal} = H_\text{liquid} - H_\text{crystal} - T ( S_\text{liquid} - S_\text{crystal} ) $$
$$ \Delta G = \Delta H - T \Delta S $$
If we suppose that this process is spontaneous then we would say that the change in Gibbs’ free energy is negative, i.e. $\Delta G < 0$. This is true if and only if,
$$\Delta H < T \Delta S$$
Traditionally we interpret this as saying that there is a thermally-driven increase in entropy when we melt a highly ordered crystal into a liquid which more than offsets the energy cost associated with the enthalpies of the interactions holding that crystal together. A chemist tends to learn early on that the reverse is not necessarily true: at some definite temperature a perfect crystal rarely forms from the liquid. This inability to just heat up any substance and always produce a perfect crystal by cooling illustrates how crystal formation is a case of kinetic- rather than thermodynamic- control. So the process by which you form your crystal could possibly result in a different crystal structure. Sometimes crystal structures change just by altering the temperature of the chamber you’re measuring the crystal structure in! Now neither of these cases apply to sodium chloride to the best of my knowledge.
The formation of an ionic crystal such as sodium chloride is a delicate balance between electrostatic attraction and Pauli repulsion. Electrostatic attraction says that between two different charges, $q_+$ and $q_-$, there is a Coulomb force given by, $$F= \frac{k q_+ q_-}{r^2}$$
where $r$ is the distance between the two charges. If one plays with the numbers then it’s easy to see that at short distances the force is strongest, but there is a limit to how close they may come together. Eventually a repulsive force due to a quantum mechanical principle called the Pauli Exclusion Principle overpowers the attraction. An equilibrium results in which the atoms sit a certain distance from one another so that, if you will humor me, the “forces” between them balance out. This is why we traditionally represent crystal packing using marbles with a unique radii. The radii of the hard marble represents where the Pauli repulsion overpowers the attraction.
You might say, “Sure, we have these kinetic, electrostatic and quantum mechanical factors to consider, but how do these help with the final crystal structure?” Hold your horses, we’re getting there.
A famous mathematician and scientist thought about the most efficient ways to pack spheres of the same size together. By most efficient I mean this in terms of what FedEx considers efficient, fitting things together into the smallest possible volume. This is also what electrostatics want. Kepler suggested that the best way to pack spheres with this in mind can be maximized in two ways: fcc and hcp.
This conjecture, however, rests on all the spheres being uniform. We can’t assume this is the same for the atoms in table salt because the cation, sodium, is said to be smaller than the chloride anion. A tool that is useful as a guide for helping predict the structure is based off of the relative size of the cation, $r_\text C$, to the anion, $r_\text A$. This radius ratio, $r_\text C / r_\text A$, is mostly only useful for simple binary species. These $r_\text C$ and $r_\text A$ are tabulated as ionic radii in many chemistry books; they are contrived by various rules proposed by researchers, such as Linus Pauling, applied to the experimentally determined interionic distances. In this system we find it useful to discuss how many neighbors an anion or a cation has of the opposite charge.
The coordination number ($\mathrm{CN}$) tells us how many neighbors a type of atom has around it. A certain $r_\text C/r_\text A$ gives us a feel for the probable CN. From one source I was able to calculate the ratio for $\ce{NaCl}$ as $r_{\ce{Na+}}/r_{\ce{Cl-}} = 0.564$. Most textbooks will say that if your ratio is $0.414{–}0.732$, then you have $\mathrm{CN}=6$. I’ll show you how to calculate the minimum value for $\mathrm{CN}=6$. The easiest way to obtain the maximum is to obtain the minimum for $\mathrm{CN}=8$.
Briefly, if we put a small cation on a $XY$ plane and surround it in the manner shown below then we could also place two circles in the $Z$-axis above and below the central cation for $\mathrm{CN}=6$. (I won’t show those two in the $Z$-axis for clarity.) We would say that a given $\mathrm{CN}$ is stable only if the spheres are all touching each other. We would say that for the ratios drawn that a $\mathrm{CN}=8$ is not stable because our cation’s sphere would be too small and wouldn’t touch all eight of its neighbors. A higher density is always preferred, so $\mathrm{CN}=4$ is not preferred when $\mathrm{CN}=6$ is stable.
Clearly, $\angle \mathrm{DAC} = 45^{\circ}$. Moreover, we can say that $\overline{\mathrm{DC}} = r_\text A$ and $\overline{\mathrm{AC}} = r_\text A + r_\text C$. Trigonometry will tells us that $$\sin 45^{\circ} = \frac{\overline{\mathrm{DC}}}{\overline{\mathrm{AC}}}$$ Substituting in those values just determined,
$$\frac{\sqrt{2}}{2} = \frac{r_\text A}{\overline{r_\text A + r_\text C}}$$
Solving for $r_\text C/r_\text A$ gives,
$$r_\text C/r_\text A=\frac{2}{\sqrt{2}} -1 = 0.414$$
which is the lower bound desired.
So we can say that the $\mathrm{CN}=6$ for the cation. Similarly you can figure out that $\mathrm{CN}=6$ for the anion. This is why we say it has a 6:6 coordination pattern, which is indicative of a sodium chloride type structure. Cesium chloride, on the other hand, is 8:8 and indicative of its structure type. We tend to say that these patterns are consistent with a certain crystal structure because all the pieces fit together consistently. This geometric concept is based purely on sphere packing and is not without its limitations.
A more quantitative way to figure out which geometry is preferred is to calculate the electrostatic energies for the different geometries. A Russian by the name of Kapustinskii derived a formula to do this,
$$E_\text{lattice} (\mathrm{kJ/mol}) = \frac{\alpha}{r_\text C + r_\text A} $$
From here you need to put it in terms of the radius ratio with a constant $r_\text A$,
(I should note that this is evil algebra: $ \frac{a/b}{d/b + 1}= \frac{a}{b+d} $ where $a=\alpha$, $b=r_\text A$, $d=r_\text C$.)
$$E_\text{lattice} = \frac{\alpha/r_\text A}{\frac{r_\text C}{r_\text A} + 1} $$
Basically $\alpha$ is different for a change in the crystal structure type and from there you compare which has the preferred energy. You can reproduce the geometric trends using this more quantitative model. There are problems with this approach too, such as the neglected dipole and quadrupole and covalency. Yes, even in $\ce{NaCl}$ there is some covalent character. I think we say that $\ce{NaCl}$ $67\ \%$ ionic character. These become important when two different crystal structures are close in energy or when more exact calculations are required.