New Zealand's popular prime minister John Key has stunned the nation by announcing he will resign, after eight years as leader.