If you got to the interview stage, you probably aren’t doing anything wrong. The reality of today’s job market is that employers have the advantage because there’s an oversupply of candidates; paring down a large list of applicants is difficult enough, discerning who should be a job in a final pool of 3 or 4 highly qualified applicant is a nightmare.
It’s always good to remember that people don’t hire CVs: they hire other people. This means there’s a human element that enters in the selection process, and at the end of the process this human element can rarely be controlled, lest you are obviously much more qualified than the competing candidates. Basically, it can come down to pure luck.
You might have been bested because the other candidate was a slightly better fit for that specific position, because the supervisor of this other candidate or a referee for this other candidate knows the person offering the position, because the person offering the position has historically had good candidates from the school of this other applicant, because this other candidate publishes in this or that journal that is favoured by the employer etc, i.e. a whole host of factors which have minimal weight until someone has to decide between two more or less equally qualified candidates. Maybe this other candidate had just one more citation than you did at the time of the decision.
You cannot change any of the above factors; it’s unlikely that people who get the positions do something really different. If it’s just bad luck then one can take solace in the fact that there usually is a very restricted number of candidates on the final shortlist, so that every time you make it to a shortlist you increase the chances of all these infinitesimal variations will go your way this one next time.