Why would temperature affect the energy of each gallon?
The energy content depends on the mass (i.e. on the number of molecules available for combustion)
The volume of a kilogram mass of gasoline depends on it's temperature - gasoline expands and becomes less dense as it gets warmer. So a litre of warm gasoline contains less mass than a litre of cold gasoline.
The difference is very slight and underground gasoline storage tanks maintain a fairly even temperature day and night. The difference may be less than the volumetric accuracy of the gasoline pump.
When gasoline is delivered in large tanker-trucks, the temperature is taken into account when calculating the value of the delivered volume.
What would be the optimal conditions to fuel your car so that you can get the most energy per gallon?
As explained above, in practice this isn't worth doing. A better strategy is to drive smoothly, plan ahead whilst driving, use throttle and brakes as little as possible, use highest possible gear, don't use high speeds.
An overly literal answer might be:
At the end of your previous journey, remove the fuel from your car and put it into a chiller. Just before your next journey put into your tank just sufficient mass to complete your journey. By using chilled fuel, the volume is less so the number of gallons is fewer - this however does not save money or fuel (as measured by mass) it just gets you furthest using the least number of gallons.
By not carrying fuel you won't use, you reduce the mass of the vehicle and it's contents, less mass means less force needed to achieve a specific acceleration, which means a reduction in fuel consumption. This doesn't increase the energy per gallon but it does save you money.