How to proceed after failing exam to advance into PhD program, and subsequently losing motivation?
Two things:


A single exam is a high-variance way to measure the ability and potential of a complicated and multifaceted being called a human.  In other words, I wouldn't take a single failure too seriously.  Sometimes you have a bad day; some people simply don't perform well on tests even if they perform well "in real life."  And even if it turns out that you are less qualified than your peers according to the traditional standards of the field, that doesn't mean you don't have something unique and valuable to contribute.
Is there anything in your life right now that you're excited about?  If not, you might consider external factors.  Some days I'm fed up with my problem, and my research, and my career.  But after going for a run, eating well, spending time with friends, seeing a thought-provoking movie, and getting a good night's sleep, I often find (much to my surprise) that I am excited about my life and my work again.  In other words: don't underestimate the value of external factors.  (Especially exercise.  I cannot stress that one enough!)