The papacy has traditionally distinguished favored Catholic monarchs (kings, more precisely) by conferring upon them certain honors and privileges, such as the right for queens to wear white instead of black during papal audiences (the privilège du blanc), or the honorific Fidei Defensor (Defender of the Faith).
One of the most prestigious of such awards was (that) of an ecclesiastic (i.e. religious) style, typically unique to the crown being so honored. You may, for example, be familiar with the instance of Isabella and Ferdinand in Spain -- and all subsequent Spanish monarchs -- being styled (Most) Catholic Majesties, from 1493 onwards.
Apostolic Majesty is 'simply' another instance of this sort of honorific -- in its case, unique to the kings of Hungary, just as the kings of Portugal were distinguished as Faithful Majesties, etc.
The 'official' hereditary grant in virtue of which Franz Joseph ultimately inherited it, came in the form of papal letters patent issued by Clement XIII in 1758 to honor Maria Theresa.
Now, those are two separate titles of Franz Joseph.
"Imperial Majesty" and "Royal apostolic majesty", the Imperial Majesty refers to his title as Austrian Emperor and Royal Apostolic Majesty refers to his title as King of Hungary.
Apostolic kingship being a feature of the kingdom of Hungary, roughly analogous to the similar titles of European monarchs, like for example the Spanish royal address of "Catholic Majesty" and the French "Most Faithful majesty"*. That is to say they are titles of prestige, usually awarded by the pope for some act of faith by a previous holder of the title, and then taken along with the rest of the titles by their successors.
In the case of the Austro-Hungarian monarchs it's a title that supposedly spring from the first king of the Hungarians, Stephen of Hungary. Who unified the Carpathian basin and took on the title of King of Hungary rather than the title of Grand Prince as was the custom. He also chritianized the kingdom and established six archbishoperics. Stephen of Hungary began calling himself King to show his power over the region, and his coronation was okayed by both the Holy Roman Emperor and the Pope. Now the fact that he Christianized the kingdom of Hungary that he also established, supposedly inspired the pope to grant him the title of "Apostolic king" rather than just "king". To signal the allegorical link between his acts of christianization and that of the apostles of Jesus Christ who wandered the earth bringing Chrstianity to the world.
Now I say "Supposedly" because I don't think there's any evidence he ever used the title, and it's further complicated by the fact that he became the focus of cult and saint worship when he was canonized at the behest of his grandson. (I can't actually think of many saints except of course the Virgin Mary who was the object of as much "direct" worship as he was) Which tends to muddy matters.
But at any rate, the Habsburg Dynasty, having inherited the title by virtue of a lot of the other contenders dying in battle (And the other pretender, John Zapoya, being unable to remove him from the throne, and his successor failing to give his planned successor the crown of Hungary needed for legitimate succession), and being related to the dynasty.
Although honestly the only relevant bit is this: The Holy Roman Emperors, after a while (As in two hundred years later or thereabouts), argued to the pope that they should be allowed to use this title as it was the legitimate title of the King of Hungary and the pope said "Sure".
Now this is partly relevant because the title of Apostolic King was bestowed by the pope, whereas the title of King was one that the Emperor himself had influence over.
*Although to the English speaking world the most famous of these is "Defender of the faith", the title given to Henry the 8th by the pope, and used by his successors to this day.
This was just a short overview, and I'm sure someone with more expertise in Hungarian history will come along to give a much better one.