Fats have the most energy.
The reason for this is the amount of oxidation that takes place as these compounds are converted to carbon dioxide and water. Fats require more oxidation to become CO2 and H2O than do carbohydrates. Roughly speaking, carbohydrates already have one oxygen for every carbon atom, thus, each carbon atom needs only one more oxygen and each pair of hydrogen atoms needs one more oxygen. However, almost every carbon atom in a fat molecule needs two oxygens instead of just one additional one, and each pair of hydrogen atoms still needs one more oxygen. So, just from counting the number of oxygens needed to be added, fats require about half again as much oxygen for the same number of carbon atoms. Because of this, the oxidation of fats takes longer, but it also gives off more energy.
When comparing gram to gram, instead of carbon to carbon, the effect is exaggerated. When you weigh a carbohydrate, more oxygen is included in that weight. When you weigh a fat, you get more carbon atoms per gram and therefore, gram for gram, the fats will give even more energy (over twice as much) than will the carbohydrates. Generally, fats provide about 9 kilocalories per gram and carbohydrates provide about 4 kilocalories per gram. (Using nutritional units, that is 9 Calories/gram for fats and 4 Calories/gram for carbohydrates.)