Trump has managed to pull off an improbable defeat despite having started with an overwhelming advantage