What caused a surge in the population of the United States from 1800-1840, making it go from 5 to 17 million people?
Basically, what caused this population expansion was the simple fact that the United States was a prosperous place to live. It had lots of land for people to live and farm and so people were able to get enough to eat. This helped them live long enough and in good enough health to have a lot of children who also tended to survive.
What this means is that there were no real events that caused the population gain. (By the way, I have changed the numbers on your question because the official population in 1840 was 17 million, not 20 million people.) In fact, the rate of population gain in the US was not much greater during the period from 1800 to 1840 than it had been in the last 20 years or so of the colonial era.
By 1840, there had also not been enough immigration to really affect these numbers. The estimate is that about 98% of the population in 1830 was native-born, meaning that immigration was not yet a major factor. This would not change until the 1840s.
So, the only thing we can attribute the population growth to is natural increase. There were not any huge breakthroughs in medicine during the time so it is simply due to the fact that the US was a healthy and prosperous place to live.