There is a high probability that we will have plenty of time left on Earth before it becomes necessary to move on. It is hard to assign a specific number of years we will have, but IMO it's most likely we will have at least a number of years in the millions. It is possible we end up with a billion, and also that we only have a thousand. It is however extremely unlikely that we have less than a thousand. Within even a hypothetical very low number of years left for man to live on earth, there is an enormous amount of time in terms of technological development, especially considering how advanced we are already. It is likely that at some point before we need to leave, we will have robots capable of self replication, able to automatically mine sources off and on Earth and provide mankind with the resources it needs on or off of earth. We can expect advances in genetic modification of food sources. We could use algae culture to help us feed ourselves, hydroponic gardening etc. We could genetically modify strains of plants more suitable for growth in space. It is entirely possible in the future to support a very impressive selection of foods even in space.
When it comes down to leaving Earth as a necessity for human survival, we might not leave with a population of billions of people. There might be a new dynamic between necessary resources, space, and ability to provide this in mass quantities. However, the technology I mentioned earlier may change that. If we have resources mined from all over the solar system handed to us on a silver platter via automated robotic workforces, we could technically support vast populations, although you can never guess what the political environment and power structure will be like. Will man emerge as one, or as various factions. If we emerge as multiple factions, it is likely that each faction will have the potential for mass destruction, and the factions will likely be sustained by threat of force just as they are now. There are however various ways you could imagine the future playing out. Will one faction gain an advantage and strike down the others and emerge alone?
In terms of human health in space, there are still unanswered questions we have. It may be the case that when this time comes, we will genetically engineer ourselves to be more hearty for the task. There may be threat of disease. We may have nano robots constantly repairing damage to our bodies and destroying unwanted viruses or bacteria. The effects of zero g, and of exposure to radiation in space may be a health threat over long periods of time. I don't know if these two problems will become irrelevant with the advances of technology or not, but I find it likely.
If we have cities built on other planets or moons, we have the gravity problem taken care of. We could even live on a hostile Earth using the same types of methods we would in space, or on other planets. We could build underground cities as well, both on Earth and on other planets/moons. Living underground, will also help solve the issue of not having an atmosphere or magnetic field to protect us from cosmic rays.
When it becomes impossible to live on earth, it will likely because over the course of billions of years, the sun will expand and even one day engulf the earth. We can't say what the solar system will be like by then, but we can hope that at that point other planets further from the sun will warm up and we may have more friendly planets or moons to make home in our solar system.
Billions of years later, when the solar system is no more, or unsupportive of us, we will face the challenge of moving to a new solar system. Hopefully, by that time we are so advanced that this will be no problem for us. Personally I think that migration out of the solar system, if required in 200 years, would probably be doable.
I can't find any problems we should run into that are likely to be unsolvable. There is the possibility, I guess, that we could destroy ourselves, or set ourselves back to the point we don't pull it off, or lose our technological means to do so.