How are the 4-digit squawk codes assigned to individual flights?
This answer indicates that there should be a "correct" one. How does ATC know what is "correct" and what is "incorrect"?
To start out with, I'll be referring to how the US does it, and it's generally laid out in the National Beacon Code Allocation Plan. Every facility has various blocks of codes that they can use to assign to flight plans, as described in the linked document. For example, a smaller tower/approach control, could use the codes 0101-0177 for IFR flights that are only in their airspace, and 0201-0277 for the VFR flights that stay in their airspace, and a Center would have a similar bank of codes for flights just in their airspace, and then a different bank for codes that go to different centers. The whole purpose to these allocations, is to minimize the need to change the squawk code while in the air, and to avoid overlap with adjacent facilities.
As flight plans are submitted to the various computers, about 30 min before an Instrument flight plan's proposed time, it's officially assigned a code from the available blocks, and it's printed at the departure airport or facility that'll first work it. Flight plans stay in the system, depending on however long the center sets. Often it's 2 hours, sometimes 3, but during bad whether when lots of flights are being delayed, it could be extended to 4+ hours. The drop interval helps to keep enough codes available for aircraft intending to depart, while allowing flight plans to go away if something happens and the flight won't be departing.
If the aircraft just randomly calls ATC, the controller types it in to either the radar or flight plan system, depending on what extent the pilot wants services, and then the system prints out an appropriate beacon code.
A squawk code (aka Mode A code) is used in Flight Data Processing systems to correlate a radar track (or other surveillance system track) to a flight plan. Basically the radar downlinks the squawk code from the aircraft and the FDPS looks up the associated flight plan. Usually there are a number of codes which do not link to a flight plan but are reserved for local purposes and VFR flights without a flight plan.
In Europe there are 3 Mode A code allocation systems in use:
Under ORCAM aircraft are assigned a code on departure and will maintain this code until they fly into a region where their code conflicts with another flight. Then it is reassigned. For every region there are 'local' codes which are only used for regional / domestic flights, and there are 'transition' codes for region boundary crossing flights.
In CCAMS a computer system allocates codes to flights based on the planned trajectory through the CCAMS area and ensures there will never be a code conflict.
When using ELS, if a flight needs to be (re-)assigned a mode A code and it is in Mode S radar coverage and the downlinked aircraft ID correlates with the filed flight ID on the flight plan, Mode A 1000 is assigned. Squawk 1000 serves as a signal to the Flight Data Processing System to correlate the radar track to the flight plan based on the downlinked Mode S aircraft ID instead of the Mode A code. This method is usually used in combination with ORCAM, sometimes with CCAMS. By using ELS, multiple aircraft fly with squawk 1000 which frees up other Mode A codes.
This Eurocontrol document (PDF) gives some more detail about the code allocation schemes in use.
South America and the Caribean use the ORCAM (PDF) system.
In the US, the codes follow section 5 of the 7110.65. Non-discrete codes end in 00 and can cover multiple aircraft. Discrete codes never end in 0 and are specific to one aircraft.
"How does ATC know what is "correct" and what is "incorrect"?"
The computer system generates them. There are certain "slot ranges" for every facility. It's not that the controller punches in something he came up with on his own. He requests a squawk from the system, and then the computer spits out one, and that's the one he/she tells the pilot over the radio. To answer your question on how it knows correct vs incorrect, I'd have to say that this is implemented correctly in the software that runs the backend.