Over 100 years ago Wilbur Atwater did the pioneering work that gave a then good enough approximation of how much energy in various types of food was available to the human body. It is easy to put food in a calorimeter, combust it, and figure out how much energy was released - what he did was much more difficult and interesting - naming working out how much energy was available to us for some of the major components of foods.
There are many issues with Atwater's findings, but they are "good enough" for many approximations and slight modifications have been codified into nutrition labels. Specifically fats provide 9 Calories/gram and carbohydrates and proteins 4. (fiber, alcohol, organic acids and polyols are also measured). When a food maker creates a new food all they have to do is sort out the amount of each major food element and then calculate the total Caloric content per unit of weight from these Atwater numbers.
There is a huge debate these days centering around this - if you want better than 10% accuracy what is really going on in human metabolism? Many people interested in weight loss claim energy is not conserved. That isn't true - physics still works. The problem is we don't understand what our body is doing at a deep enough level.
Not a huge amount of progress has been made on refining Atwater's work, but some strides have been made and recently much more work is underway. An interesting line of research centers around tree nuts. It has noticed that a person can eat a small amount of nuts and not see the caloric impact that the old numbers imply. Some hypotheses have been made - for example: nuts may contain a lot of a type of fiber that blocks fat absorption by the body. But now deeper work is being done - some work on almonds is summarized in this paper (pdf)
Discrepancy between the Atwater factor predicted and empirically measured energy values of almonds in human diets
Janet A Novotny, Sarah K Gebauer, and David J Baer
Background: The energy content of foods is primarily determined by the Atwater factors, which may not be accurate for certain food groups. Nuts are a food group for which substantial evidence suggests that the Atwater factors may be poorly predictive. Objective: A study was conducted to determine the energy value of almonds in the human diet and to compare the measured energy value with the value calculated from the Atwater factors.
Design: Eighteen healthy adults consumed a controlled diet or an almond-containing diet for 18 d. Three treatments were administered to subjects in a crossover design, and diets contained 1 of 3 almond doses: 0, 42, or 84 g/d. During the final 9 d of the treatment period, volunteers collected all urine and feces, and samples of diets, feces, and urine were analyzed for macronutrient and energy contents. The metabolizable energy content of the almonds was determined.
Results: The energy content of almonds in the human diet was found to be 4.6 6 0.8 kcal/g, which is equivalent to 129 kcal/28-g serving. This is significantly less than the energy density of 6.0–6.1 kcal/g as determined by the Atwater factors, which is equivalent to an energy content of 168–170 kcal/serving. The Atwater factors, when applied to almonds, resulted in a 32% overestimation of their measured energy content.
Conclusion: This study provides evidence for the inaccuracies of the Atwater factors for certain applications and provides a rigorous method for determining empirically the energy value of individual foods within the context of a mixed diet.
The difference is about 25% - the largest correction I've seen so far. Some nuts are healthy and most of the fat turns out to be healthy, so eating them in moderation is probably a good thing. I'd be curious about the other tree nuts as well as peanuts (which aren't a nut, but a legume)