When Madara was fighting with Hashirama, he secretly bite him and took some of his cells. According to a philosophy one who posses both Indra and Ashura chakra can only open the Rinnegan, members of Uchiha clan were descendants of Indra and members of Senju and Uzumaki clan were descendants of Ashura. Madara already have Indra chakra in him he implanted Hashirama’s cells in him and got the Ashura chakra as well that’s how he was able to awake the Rinnegan.
After being defeated by Hashirama Madara used Izanagi to fake his death. After this he choose Nagato Uzumaki to behold his Rinnegan because Uzumaki clan have lot of chakra energy in them and Madara believed that he could hold the power of it.
When rock fell on Obito, Madara brought him to his hideout and repaires his damaged body parts with White Zetsu’s. Madara also told Obito that his body passed through the rocks – which means Obito’s Sharingan developed a new power to pass through things -Kamui-because he was about to be crushed by rocks. Obito actually didn’t wanted to join Madara but when he see Kakashi killing Rin he felt hurt and joined Madara. Madara died giving his name to Obito and handing him the job to revive him at the required time.