As far as I understand, the emperor of the HRE was formed as a catholic german union. But how did this work out with protestantism? I mean i know that it was the reason for the 30 years war, but after the war, how did protestant states fit within the catholic HRE?
Somehow they fitted quite well, although they kind of destroyed it in the end. The HRE wasn't so much seen as a "catholic german union" neither before nor after the 30 year war.
First it wasn't (meant to be)german but the continuing roman empire.
Second the emperor was of "gods grace"(represented by the lord electors) , not of the popes, a fact that led to a lot of wars between pope and emperor about a real swarf of topics where it always came down to the question of final authority. Although the emperor took the popes side in the Lutheranern question he sacked Rome a little in a Italian territorial dispute. In fact the siding with the pope was a strategical political decision; the house of Habsburg rouse far beyond being a Primus inter pares in the HRE. the amount of land, people, money and by this armies they were able to sustain outnumbered almost every other ruler in the whole of Europe but wasn't really mirrored in its political weight in the empire, a problem that was to be solved. So to the Emperors side, beside the religious question, it was also a war which should centralize the empire and enforce some kind of absolutism with the house of Habsburg at the top.
With the westphalian peace not only the religious unification but also the forming of a more (modern) centralized state for the HRE was of the table with its smaller states (the catholic as well as the protestant) more independent than ever.