Re-answering this question because I don't really like what I said ten years ago.
Before starting, eject and reinsert the game card, clean the pins on the cartridge (cotton swab with alcohol), and try using a different console if possible. This excludes some fixable issues unrelated to the save file.
If you determine the issue is indeed related to your save file, your best chance to recover it is to dump it out then open it in PKHeX, which is a third-party save file editor. If you have an authentic Gen 4-7 cartridge plus a modded 3DS, you can easily dump your save files to an SD card using Checkpoint then access them on your computer. Gen 8+ can be done with Switch hacks, but that console is harder to deal with.
The most likely reason your save file is corrupt is it failed a checksum (more info), which PKHeX will ignore and allow you to fix. Simply import the save file to PKHeX and then export (File -> Export SAV...). This will correct the checksums, but it will not magic away any damaged data, so you may find problems with your game when you play it again.
For example, I loaded a corrupt HeartGold save file into PKHeX. SAV -> Verify Checksums confirms the problem, and so does a look through the PC boxes:

Inspecting the red squares further reveals more data that is obviously bogus:

As promised, I used PKHeX to fix the checksums and load up this save in my game. Below on the left is what BOX 6 looks like. The game crashes when hovering most of the "eggs", but some are more stable and can be interacted with, as seen on the right. It seems like the bugged Pokemon loaded junk data into the RAM which stuffed up the menu text.

Who knows what else (say, story event flags) has also been affected that could make the game unstable/unplayable. So, like I explained, this is a superficial fix: you can correct the checksums and make the game load the save, but you can't fix what the game was trying to protect you from.
If your save file is very badly damaged e.g. truncated or completely wiped, there is nothing you can do at all. This is easy to identify by opening the save file in a hex editor, which would show a lot of zeroes. PKHeX will also refuse unreadable saves like these.
If you do recover your save file, use your computer to copy it into Checkpoint's saves directory on your SD card, then write it back onto the game card using Checkpoint.
In terms of advice for avoiding corrupt save data:
- If your games are saved to an SD card, make sure it's not cheap/faulty as failing flash memory will corrupt your data.
- If you have a fake/reproduction cartridge, don't expect a smooth experience like you would get with the real thing. Fake cartridges are notorious for having faulty flash memory and for issues like "game freezes after doing x", due to unfixable issues with the ROM.
- If you have a flashcart (e.g. R4 or TT), similarly do not expect the same experience as with the real thing, and make sure you get a new microSD card for it from a reputable brand.
- Reputable save file editors and cheats won't cause corrupt data, but if you don't know what you're doing with them, your risk is higher.
- Don't turn the console off while saving
- Don't mistreat your cartridges
- Store your cartridges somewhere safe
Once-off, freak accident errors can happen to careful people. The only way to mitigate against this, as with all things digital, is to make backups.