XRD is mostly useful for crystals. What makes a material a crystal? A material is a crystal if the atoms/molecules are arranged in a periodic pattern. The dots below make a square pattern, so if they were atoms, they would be a crystal.
Or you could arrange the atoms in a different pattern. They would still be a crystal as long as there is a clear pattern to their arrangement. The following is a hexagonal arrangement.
In a complex crystal structure, there may be multiple ways of measuring the distance between atoms. In the picture below, you could measure distances between atoms along the blue line, along the orange line, along the green line, or along the pink line. You can see that the distances are the same for the blue and pink lines, but the distance for the green line is longer, and the distances for the orange line are even longer.
The central thing that XRD tells you is what the distance is between atoms, so it provides the way to determine a crystal structure. So how does XRD measure atomic distances? By diffraction. Diffraction is when a wave bounces off a periodic structure. The wave will bounce off the periodic structure and will reflect in a direction that depends on the spacing of the pattern. In XRD, the wave that is used is an X-ray. An x-ray is just light with a very short wavelength. The x-rays I use have a wavelength of 1.5 Angstroms (1.5 x 10^-10 m). Diffraction only works if the distance between atoms is close to the wavelength of the wave. Conveniently, atoms are usually spaced a few angstroms from one another.
If you shoot an x-ray at a crystal, it looks something like this:
One x-ray bounces of one atom and another x-ray bounces off another atom. You can see that the two x-rays travel different distances. The top one travels a shorter distance (2dSin(θ) shorter than the lower one). The difference in paths cause a phase difference between the two x-rays. When they head towards the crystal, they wiggle up and down at the same time. But after travelling different distances, they may or may not be wiggling up and down simultaneously. In the above picture, the difference in paths is just right so that the two waves are wiggling together after reflecting. Waves add together, so you can add the two waves together and see that you get a nice big wave twice the size of the original ones. This is called constructive interference. This means that you would see a bright spot if these waves ran into a piece of photo paper.
But if the difference in the paths was not just perfect, the waves could have ended up deconstructively interfering, which means you would see a dark spot on a piece of photo paper. One way you could get this is by changing the angle that the x-rays come in at.
In this case, you can see that the x-rays are wiggling together on the left, but after they reflect, they are wiggling opposite of one another. If you add those together, you get a flat line, which would result in a dark spot. What this show us is that for a certain crystal structure, x-rays only reflect off at certain unique angles.
The other way that you can change the paths between the two x-rays is by changing the atomic spacings.
In this case, I moved the two planes away from each other, so when the x-rays bounce back up, they deconstructively interfere. What this shows us is that x-rays bounce off at certain angles dependent on the atomic spacing. This is the key to x-ray diffraction. It means that if you see an x-ray bouncing off a crystal at a certain angle, you know exactly what the spacing is between the two atoms it bounced off. Going back to what I said before, if you know all the unique atomic spacings between different atoms in a crystal, you know what kind of crystal structure it is.
When you actually take an XRD measurement, you end up with a graph that looks like this:
Each peak in this graph tells you what angle the x-rays are reflecting at. If you know the angle they reflect at, you can figure out what the atomic spacing is by Bragg's law:
For the first peak in the graph, n=1, the wavelength λ=1.5418, and the angle θ=18.8/2=9.4 degrees. If you solve that equation, you get that the atomic spacing for the (003) plane is d=4.72 angstroms. The (003) plane is analogous to the colored lines that we drew up above. For instance, the blue line would show the atomic spacings for the (10) plane, the red line would show atomic spacings for the (12) plane, etc. So that is how XRD uses diffraction to determine crystal structure.