David Cameron says he will step down as prime minister before October after Britain voted to leave the European Union.