Starting with Fredrick III in 1415, the Habsburgs maintained the tile of Emperor of the Romans for more than 300 years, despite the devastating 30 Years' War, which devastated them financially, and I thought would turn many of the electors against them. I've also read that outright bribery, while common for the election in the Medieval period, somewhat died out in the Early Modern Period, which leaves me even more confused considering the main advantage I see the Habsburgs having for most of this period is the immense wealth of the Spanish Empire.
How exactly did the Habsburgs remain Holy Roman Emperors for 300 years, despite having been in likely the most devastating civil war in European history?
I've drafted a response to a similar question in the past, but I--unfortunately--can not locate it. So, alas, more work for me.
To identify why the Austrian Habsburgs were so succesful in retaining the Imperial title, let us first consider what it actually meant to be Holy Roman Emperor, and what was required of the man who received the title. The most impotant thing to remember is that the Emperor was supposed to simultaneously be able to maintain peace within the Empire, and also to defend the Empire against hostile threat. Furthermore, the Emperor was supposed to have a kind of "Emperor-ness" about him that in turn made it so that to be even considered as a viable candidate for the Imperial throne, you would have to hold a certain degree of prestige already. Combined with the fact that--while the Imperial Diet could vote for taxes to fund armies--the Emperor was expected to foot most of the bill of wars to preserve or defend the Empire himself. This meant that, to have an effective Emperor, he would need to rule a large, prosperous land. While Austria is usually noted for having a less efficient administration than many of the other European states, within the Holy Roman Empire, the Habsburg rule over the Archduchy of Austria, Styria, Tyrol, Carinthia, and--of course--Bohemia, made them the largest single prince in the Empire in terms of territory controlled, and thus one of the most able to marshall the necessary resources to provide for the wider defense of the Emprrie. This made them a natural choice for the position of Emperor. In the time of the Reformation, that they were Catholic also made them an easy vote for the three ecclesiastical electors, while moderate Protestants (like the Elector of Saxony for much of this period) were willing to vote for the Habsburg candidate to maintain order in the Empire, and negotiate for the insurance of their vote rather than attempting to try and find a way to force through a Protestant Emperor. Even in the election of 1618, where Bohemia was in open revolt, and Frederick V, the Elector-Palatine trying to push for a non-Habsburg monarch, Saxony and Brandenburgh supported the Catholic Habsburgs for the Imperial throne (and so did--ironically enough--Frederick's own representative).
So, the biggest advantage the Austrian Habsburgs had within the Imperial election was not necessarily the wealth of their Spanish cousins (as much of Spain's wealth was being spent on its own concerns, and Spanish-Austrian cooperation was often fraught, even throughout the Thirty Years War), but rather the size of their holdings within the Empire of their own accord. Austria and Bohemia were wealthy provinces, and--united under Habsburg rule--formed a significant power base to carry out the duties of Emperor that few other potential candidates could turn to.
To move on to the part of your question more concerned with post-Thirty Years War policy, it's an oft repeated claim that the Thirty Years War defeated Habsburg ambitions and power in Germany, and re-directed their attention into their Balkan holdings. However, this isn't really the case. Contrary to your expectations, int he aftermath of the Thirty Years War, many of the electors (and the Empire as whole) tended to rally to the Habsburgs, actually increasing Habsburg influence in the Empire throughout the late-17th and early-18th centuries. While the Thirty Years War was indeed devastating, it was devastating to everyone within the Empire. However, in the aftermath, many of the Imperial Princes began to see the Austrian Habsburgs as a potential safeguard of their security against the burgeoning power of France. Maintaining Habsburg rule of the Empire, and even voting for raising of Imperial armies and taxes to support the Habsburgs militarily became ever more prevalent. In some ways, this was partially because the Habsburgs were seen as a weaker force, which could not necessarily exert the same amount of power needed to overcome and completely dominate the smaller Princes of the Empire, in the same way that France would. In short, it would be better to be an Imperial Prince under a distant and limited Habsburg Emperor than it would be to be a province of France under a far more absolutist King.
So, to sum it all up, the biggest reason the Habsburgs retained the Impeial crown for so long without interruption was frankly because--for most of the period--they were simply the best candidate available. Being Emperor required a great deal of personal resources, which the Austrian Habsburgs had via their holdings in Austria and Bohemia. Later on, they were a preferrable counterweight to domination by outside forces, while also having their own expanded power base (considering their new lands claimed in the Great Turkish War).
Hopefully this has helped to answer your question. Please feel free to ask any followups.
Sources
Michael Hochedlinger, Austria's Wars of Emergence, 1683-1797
A. Wess Mitchell, The Grand Strategy of the Habsburg Empire
Peter Wilson, The Thirty Years War: Europe's Tragedy
Peter Wislon, *Heart of Europe: A History of the Holy Roman Empire