TF2 manages the number of sentry guns available in a very strange way. That is, it can be easily changed and modified by the server. I assume this is for debug purposes to allow developers to test multiple sentry positions at the same time.
Unfortunately, the system that is used can be easily broken by sentry guns being removed by the engine without being built (such as when you return a gun to a cabinet and change to a different weapon.) Normally this would make it appear to the game that the engineer is constantly capable of building his sentry gun because it never said he actually placed it down. As long as he doesn't actually place one without the debug build command, it will allow him to infinitely build guns.