The other two repsonses to this question are right on, you need to consult with a real estate attorney, not just an attorney. Make sure the attorney is versed in real estate law. There are contingencies in a contract to protect the buyer and the seller. It depends on how far through those contingencies you are. But, as the buyer, you have your financing contingency to protect you. If you cannot get a loan now because of a job situation, you can send a release form to the seller side and proof from your lender that you no longer are approved for your loan. Again, consulting an attorney is in your best interests. I hope everything works out for you.
I will agree with Chad, you need to talk to your attorney and agent and explain your current situation. You are not losing earnest money on the transaction of the sale of your home since it is the buyer's earnest money, and not yours to keep. And if you have a contract to buy and no longer qualify for a loan, there is a financing contingency in your purchase contract to protect your ernest money.
When did you sign the contract? You might still be within the attorney review period, in which case it's not a problem to back out. Are there any other contingencies? Are you still negotiating any repairs or credits? .... your attorney will now best how to advise you!