In terms of games, it normally extends from the player having choices, these choices affect character relationships and events which change the the story to try and give a normally linear plot some openness or when there isn't too much game play involved.
- For example, in Fate/Stay Night, in the Heaven's Feel Route, if you don't build up enough of a relationship with Sakura, when the time comes you are unable to go against your ideals of being a "Ally of Justice" and get a bad ending. By building up on the relationship, you abandon your ideal and instead stay by Sakura's side and help her
In the example above, the endings are decided by choices you make. In some cases, it's how you play the game:
- For example, in Mugen Souls, by increasing the level of all of Chou Chou's forms and then viewing all the scenes in the Mugen Field where her different forms take over and speak, you see a final scene where all her forms reveal that they feel now that they no longer have a purpose as they were created by Chou Chou to fill the loneliness she felt, and now she had so many friend, they wasn't needed. However, the crew tells them that Chou Chou would still be sad if they disappeared and the decided to remain, and this leads to a scene in the final battle where Chou Chou plans to sacrifice herself to save Belleria from Vorgis, but is stopped by her other forms, since the level of these other forms is dependent on the number of Shampuru you have collected you also hears the voices of the people that have been turned into Shampuru and however even though they were forced into servitude they will fight and serve Chou Chou loyally and launch a separate attack at Vorgis to weaken him ready to the final-final battle
There are also some games tend to only have 1 ending and numerous bad endings as like a game over, these are normally backed up by a mechanic that enables the choice to proceed further into the game.
- For example, in Sekien no Inganock ~What a Beautiful People~, if you don't form a solid image of both characters in the Inner Voices section in most chapters you will not have the 3rd option to extends "your right hand" and the options you have will always lead to a bad end (exception is the REBORN edition which you have the option to skip this section).
In terms of origins, while I'm not 100% sure, the earliest I have seen is in visual novels which were practically just text based with CG in the background so the purpose was to make a game that was still interactive.
Nowadays, unless it's a normal text based visual novel like Canvas 2, it's used as a form of replay value with a normal ending leaving the plot open-ended and the true ending bring about a conclusion, a bad ending also exists as a means to tell the player they majorly stuffed up how they played.
I should point out that all this information is first hand experience with English games and English translations of visual novels