The word derives from the old Slavic word og(ъ

r- for the proto-Magyars. Through Germanic languages, the word got into other European languages ((H)ungarus, (H)ungarn, Vengry etc.). The Slavic word is thought to be derived from the Bulgaro-Turkic Onogur, possibly because the proto-Magyars were neighbours (or confederates) of the Empire of the Onogurs in the 6th century, whose leading tribal union was called the "Onogurs" (meaning "ten tribes").
The H- in many languages (Hungarians, Hongrois, Hungarus etc.) is a later addition. It was taken over from the word "Huns", which was a similar semi-nomadic tribe living some 400 years earlier in present-day Hungary and having a similar way of life (or according to the older theories the people from which the Magyars arose). In ancient times, through the middle ages, and even today, the identification of Hungarians with the Huns has often occurred in history and literature, however this identification began to be disputed around the late 19th century, and is still a source of major controversy among scholars who insist that there could be no direct connection between the two.
Hun names like Attila and Réka are still popular among Hungarians, and forms derived from Latin Hungaria are used like in the racetrack Hungaroring (mostly due to the strong English language pressure in tourism and international matters).
Magyar is today simply the Hungarian word for Hungarian. In English and many other languages, however, Magyar is used instead of Hungarian in certain (mainly historical) contexts, usually to distinguish ethnic Hungarians (i.e. the Magyars) from the other nationalities living in the Hungarian kingdom.