well the old fashioned way was,essentially, to burn it in something called a bomb calorimeter.
Nowadays its a bit more refined - identifying the constituents, breaking them down into their food groups, things like Kjeldahl analysis to directly measure the nitrogen in food and indirectly the amount of protein.
all the science stuff you've just been told, using that they figure out calories in other stuff too, & I THINK they then decide what can see what off!! what burns what, i.e eating celery gives more energy burn so won't have a calorie value???
I could be wrong ;)