So how do they figure out all that information that goes on a nutrition label anyway?
First, the lab logs a sample into the system. Nutrition Labs are certified by various bodies, and accurate records are a vital part of that.
Then the sample will be homogenized, which is a fancy way of saying blended. Picture a giant food processor. Some of the sample is saved in a freezer, some is saved in a fridge. Smaller samples for each test are taken from the fridge sample.
Protein is calculated by burning the sample to measure its nitrogen content, which lets you calculate the protein present in the sample.
Moisture is measured by weighing the sample, they drying it completely, and re-weighing it to calculate the amount of the sample that was water.
Once the sample is dried, you can perform a crude fat measurement using Nuclear magnetic resonance spectroscopy (NMR spectroscopy). This is faster and easier than other fat analyses methods, but only gives an estimate of fat content, not what kinds of fat are present.
A drop of fat or oil is extracted from a sample and then used in a machine called a Gas Chromatograph. The droplet is vaporized at around 400 degrees centigrade and sent through a glass fiber tube. Big fat molecules go slower than small ones, so by detecting them as they exit the tube, you know what order and amount of each one there was in the fat. From this you can calculate how much saturated and unsaturated fat is present, as well as more details like Omega 3 and Omega 6 content.
Sugar content is analysed in a similar manner, but with a Liquid Chromatograph.
Fiber is measured by dissolving a sample in different solvents or enzyme baths, until only the fiber is left, which is then dried and weighed. If Carbohydrate calculation is needed, the sample is paused halfway for a drying and weighing stage.
With this set of information: Fats, Proteins, Carbohydrates, Sugars, Fiber and Moisture, the caloric content of the food can be calculated.
There are other things analyzed, but the above is the usual bare minimum for nutritional information.