The fundamental reason save files become "corrupted" is that they fail a checksum at some point. In the context of Pokemon save files, a checksum is a short block of data that is generated using parts of your real save data, after being fed through some algorithm. (See here for more detail as it pertains to Gen 4 Pokemon.)
When the game loads your save data, the game re-calculates checksums based upon the save data, and compares them against the checksums it made when it saved the same data. If the checksums don't match, then the game knows the save data was altered in some way, and says it is "corrupt".
The game is capable of loading corrupt data, but a failed checksum suggests the data is erroneous. If erroneous data is loaded, unexpected things might happen, so the game is programmed to not load corrupted data.
In Gen 4, the game stores two lots of save data. The first is your "recent" save, and the second is a "backup" save, which is the save prior to the most recent one. The game calculates and compares separate checksums for different parts of the save data structure, from both the recent and backup saves; more detail on that is here. But to answer this question, I'll summarise it in this way:
- If the game loads your recent save and finds only matching checksums, then it'll load your recent save without any problems. This is the expected/normal behaviour.
- If the game finds a non-matching checksum in your recent save, but then finds only matching checksums in your backup save, then it will report that the save file was corrupted, and then load the backup save. This is the scenario you're asking about.
- If the game finds a non-matching checksum in both the recent save and the backup save, then it will report the save file was corrupted, and then you'll need to start the game over.
OK, great, but why would there be a wrong checksum (i.e. why would the save data have unexpected alterations)? Here are some likely scenarios:
- Use of a flashcart to play the game. These often ship with cheap/unreliable memory cards and require the use of custom firmware with uncertain quality. A memory failure or error with the firmware may cause save data to be corrupted or lost entirely.
- Use of save editing software. When you use this software, you need to make sure it recalculates and rewrites checksums based on the changed data. The software is not designed to be 100% foolproof, so it is possible to create a "corrupt" file with poor technique.
- Generic hardware failure. This most often occurs for removable storage, like SD cards you insert into a DSi or 3DS. Nintendo's game cards appear to have high longevity, but no computer memory lasts forever. Make a habit of backing up your save data. You can extract save data from DS and 3DS cartridges using a hacked 3DS.
- Interrupting the save process. If you cut off the console's power while it's actively writing to the flash memory, then it's basically certain that save will trip a checksum next time you load it. Fortunately, modern games spend a very short amount of time actually writing data, so it's unlikely you'd have a problem even if you did make this mistake.
- Freak accident. Try starting the software again or reinserting the card.
Damage to (or dirt/dust on) the exterior of the cart or the console is more likely to make the console not recognise the cart at all.
Source: As above; also I study computer science, have done ROM hacking, and have used save editors