First, try to get over the shock of how much of a mess this made. Second, don’t worry, things will be fine. This problem always occurs during the most raucous period of primary fermentation. The yeast is producing carbon dioxide gas at such a prolific rate that the airlock cannot allow the gas to escape efficiently enough, hence, ejected bungs and airlocks.
This type of occurrence is common under one or more of the following conditions:
If your airlock has blown off the primary fermenter there is no reason to worry, being that the rapidly escaping carbon dioxide gas is creating an upward draft, which is keeping atmospheric air (i.e. airborne microbes and wild yeast) out of the primary vessel and out of contact with your beer.
Upon discovering this, merely re-sanitize the bung and airlock and reposition it back on your fermenter. If the fermentation is still too rigorous then a blow-off tube can be attached in lieu of the bung and airlock. Your beer SHOULD NOT be thrown out, 9.9 out 10 times everything will turn out just fine.