Speciation with gene flow and the genetics of habitat transitions