Why didn't Madara use Izanagi to counter Kaguya?
First, Madara always believed himself to be completely invincible. When Black Zetsu stabbed him, he could not even realise that he was being betrayed; it was completely out of his expectation. And it only took a very short period of time for Zetsu to gain complete control of his body.

Second, a huge weakness of Izanagi is that, it has be activated first. It is not a reset jutsu. It only can manipulate everything that happens between its activation and Sharingan going blind