Sometimes, it happens by choice. Sometimes, you don't have an option. In either case, it's important to be prepared to change jobs - because you never know when it might happen to you. It's happened to me both ways.
I've been laid-off and out-of-work at the end of a business day and had to turn in my company car the next morning.
That was a tough one! I've also resigned and managed to stay on excellent terms with my previous employers.