I've been working at the same university (that I also attended for BA and MA) for 19 years and have worked my way up from a principal secretary making less than $20K to my current job as an Associate Dean making approximately 5x that. I held 4 different positions during that time (2 in the same division, then 2 moves after that). I can't tell you how many times I interviewed for various positions where I thought I would definitely get the job only to be turned down and see someone else (outside and internal) get it. It is SO discouraging.
I will tell you something about my current job though. This was a job that I had applied for 5 yrs. earlier and made it to the final 2 candidates only to lose out to an external one. During that time, I managed to continue to work, get married, buy a house, and have two kids (while still interviewing all along for other positions). I see the job pop up again in the postings and of course apply. Because I've been around so long, I was able to find out where I stood in the process because I hadn't been called. My inside source (higher up administrator) told me that I just missed the short list and I shouldn't get discouraged. Man was I pissed! After fuming at my desk for a few minutes I wrote back my source and instead of complaining, wrote a thanks for letting me know and then a short paragraph describing while I was disappointed, I wished that I just would be given the chance for a face to face interview to tell them how great I'd be in the job because of all my experience doing blah blah blah - you get the picture.
Weirdly enough, I got a call about a week later to come in for an interview. I did admit to the director who called me (who I had interviewed with before) that I was surprised to hear from him. His answer to me was that the world works in mysterious ways. Well as you can see above I got the job (I was in for an interview on Thursday with a group of faculty, met the Dean, met the staff I'd be managing, and then Monday I got the offer.). I must say this job is awesome and worth all of the heartache and good times I experienced in my previous positions.
Don't give up. It is so rare that things are just handed to someone in the job market. You'll always have let downs and deal with duds, but eventually things should work out!