Just by looking at the code:
StageEx1 is unlocked by beating stage1, stage2 and stage3 with a combined total of less than or equal to 6 bombs and lives lost if you're playing normal mode, or a combined total of less than or equal to 5 bombs and lives lost if you are playing lunatic.
If you played StageEx1 and are playing lunatic mode, StageEx2 is automatically unlocked after beating stage4, which replaces stage5. If your playing normal mode, you will automatically play stage5 no matter what.
If you beat stage5/StageEx2 without continuing, you proceed to stage6.
If you're playing normal mode, stage6 is the end of the game.
If you beat stage6 with no continues, have a live stock of greater than or equal to 3, and are playing lunatic mode, you proceed to StageEx3.
If you beat StageEx3 and have still not continued*, you proceed to StageEx4.
*By looking at the code, it looks like all you have to do is beat StageEx3 without continuing, however there is "unnecessary" code that states you can proceed by having less than or equal to 15 bombs. I can't see how this will ever come in to effect though, since you need to pass StageEx3 for the conditional statement to be true anyway. The variable ex3_in is 1 (true) just by playing StageEx3, so the statement if(Continued==false&&extra4_flg&&(ex3_in||GetBombCount<=15)) will always be true if you beat StageEx3 and have not continued at all. The &&(ex3_in||GetBombCount<=15) seems completely unnecessary.
Another note: there are point requirements to pass each level successfully as well, however, if you do not continue at all, it is almost impossible to not reach the point requirement.
Also, I need to play this game and not quit after stage1. Wait! I can remember seeing Ran at some point! Damn, I still need to play this game and remember it.