quora answer, but sounds knowledgeable:
You have three power sources on an airplane: the engines themselves that have an in-built generator, an additional, small engine thats in the back and just for electricity, thats called the APU or Auxiliary Power Unit and third, while on the ground, theres the GPU or Ground Power Unit which is essentially a power cord thats plugged into the plane.
AC can run off all of these power sources, but:
The engines are usually not running while boarding for safety reasons
The GPU is rarely used. Also many airports dont have them for all airplane models.
So essentially, that leaves us just with the APU.
The APU however, on hot days, often cant provide enough power to effectively run the AC, which is why AC is often turned off on the ground to keep the power for other appliances.
But apart from all that, and thats the real reason for many flights: the APU is VERY often broken. And until the routine checks at its home airport, it wont get fixed as its not directly important for safety.
And well thats why the AC often isnt working before the main engines are turned on
Have a great day!
EDIT: As Marius pointed out correctly youd have a problem if the engines are off, the APU is broken and theres no GPU. You wouldnt have electricity for the Cockpit etc. Fortunately, however, every airplane has batteries capable of providing the necessary appliances with electricity! And of course, if an airline knows their APU is broken, they will be doing everything possible to get a GPU.
https://www.quora.com/Why-doesnt-air-conditioning-seem-to-work-when-a-plane-is-on-the-ground-but-works-perfectly-in-flight
Might be OK for ordinary temperatures, so here the problem was deciding that was OK on a really really hot day, whether technically allowed or not.