IIRC Stannis is introduced in the book in much the same way as he is in the show - on Dragonstone with Mel burning effigies of the seven gods, and it's from the viewpoint of Davos. Robert was installed as king as the leader of the rebellion, plus he was the eldest Baratheon brother. Also, because he was far more of a laugh than anyone else available. Didn't want to be king though, just wanted to get rid of the Targaeryens.
Stannis was well pissed off with Robert after the rebellion because he was sent to Dragonstone instead of being given Storms End, which was given to Renly, so there was no great love between Stannis and Robert.
I'd assume immediately before Roberts death Stannis would have been starting his association with Melissandre, who would have probably seen Roberts death in the flames, amongst other things, and chose to seek out Stannis. I don't think we know when exactly they were first together.
Edit to add (copy and paste job) - Melisandre came to Dragonstone because she believed Stannis Baratheon is Azor Ahai reborn and destined to defeat the Great Other the antithesis of her god. She turns Stannis' wife, Lady Selyse Florent, and several other members of Stannis’s court from the Faith of the Seven to her red god