It sounds to me that you are hooking up the keg (to the co2), charging the keg with 40psi, then disconnecting the co2 from the keg. If you are infact disconnecting that is why your beer is flat. 40 psi (althought a lof of pressure) at one time is not enough c02 for 5 gallons of beer. When I carbonate I:
1. Transfer 33 degree beer to keg via siphon (cold beer carbonates easier plus chilling the beer in the secondary kills all the yeast and every thing drops yeilding a clearer beer.
2. Put on keg lid.
3. Hook up to CO2 and slowly open valve (regulator) until 30 psi is pushing on the beer. Do not hook up you tap because 30 psi could (although I doubt) hurt your tap.
4. With the co2 still attached to the keg...shake the keg, rock back and forth on the floor, lay on side and bounce each end of the floor. As you move the beer your hear more co2 passing through the regulator and working its way into the beer. That is why if you unhooked your co2 the gas that you initial put into the keg was asorbed in an hour or too and it was not enough.
5. More about step 4... I shake or bounce keg for about 30 seconds and then I repeat step 4 about 3 times maybe with a 2 minutes in between to rest my arms!
6. I will then put it back in the frig and keep the co2 hooked up with 30 psi on it overnight. The next day I reduce regulator pressure to about 6 psi and then pull bleeder valve on the top of keg (some beer might fizz out-no worries). I do this because up until I do there is 30 psi on the beer that could hurt my tap.
7. Hook up tap, tap a beer.
* if you are unable to move keg around while having it hooked up to c02 then charge it to 30 psi, unhook it from co2 and bounce and shake. then hook in back up and you will see your pressure gauge probably reads 5 psi or so. charge again up to 30, shake, repeat 2 times.
I hope this helps