After David's face was disfigured in the car accident, he lost Sofia - the girl he fell in love with at the party - or more specifically, his chances to win her were ruined. He couldn't win her after having lost his good looks and also because his behavior had changed for the worst as a psychological reaction after what he had gone through. He was not just only a little bit ugly to look at, but also became rude and mean to Sofia, his best friend and everybody else in his surroundings. And eventually this ruined his chances to win Sofia, his love.
So the night when he fell asleep on the pavement, after Sofia had left him, he simply couldn't take it anymore. This was the last time he ever really saw Sofia. So one night, depressed and heartbroken, he came in contact with the L.E (The Life Extension Company) over the Internet. L.E is a company that mainly focuses on extending people's lifes by freezing them and preserving them in vessels so that they can live much longer (a process known as cryonics), for different personal reasons. L.E can also provide the frozen person with a Lucid Dream in which the person can control the directions and events in his or her artificial life.
So, in David's desperate attempt to escape from his pain, after having lost Sofia, he signed a contract with L.E. The following night he killed himself by deliberately overdosing prescription medication. Within an hour after this event, he was taken care of by L.E and preserved in a vessel in which he was frozen. While David was frozen, L.E could, with advanced technology, go inside the mind of David and make it possible for him to create his Lucid Dream, where David could determine, direct and control the course of his life in this Lucid Dream of his. His Lucid Dream was sculpted by him and everything is a creation of his own mind. And therefore David could be with Sofia in the Lucid Dream state, which was the reason why he signed the contract with L.E. and sold himself to their Lucid Dream option. So in order for the Lucid Dream to function, L.E erased David's real memories of what really happened when he woke up on the pavement, including his death. The memory of his death was wiped out. So in David's mind the Lucid Dream began when he was woken up by Sofia, and everything from here is sculpted by his own mind.
When he signed the contract they made the deal to give him the choice to stay in the Lucid Dream with Sofia (until science had found a "cure" to fix his face and body), or to be revived from his frozen state so he could return to a normal life (and have his face and body fixed). But it took 150 years for science to develop the technique to fix his facial disfigurements and damaged body.
So at the end of the movie, when he was confronted with the choice to wake up from his Lucid Dream or to stay in it and be with Sofia, he would wake up 150 years after he first was frozen, IF he would choose to be revived again (which he did). And since David had been frozen for 150 years means that Sofia, his best friend and all the other characters seen before David went into the Lucid Dream state had died a long time ago. And he had also lost his wealth during these many years. Regarding the time when his Lucid Dream began, one possible explanation is that the artificial life in David's Lucid Dream was experienced by him as having occurred only for a few weeks, while 150 years had passed in the real world. Another possible explanation is that his Lucid Dream started 150 years after he was frozen, but weeks before he was revived, and that the "splice" (the period when David was frozen but when the Lucid Dream had not yet begun) had occurred for 150 years. At the end of the movie, when he's still frozen, he is in a pause state of his Lucid Dream where he makes the choice to go back to the real world again.
And to answer this question: But if David could control and direct every event in this Lucid Dream of his, why did horrible things still start to happen to him, like him being arrested for murder and assault etc? The answer to this question is: When David was frozen in the Lucid Dream state his subconscious played tricks on him, and the Lucid Dream turned into a nightmare. The subconscious is a very powerful thing. But as we can see in the end of the movie, this was corrected.