A hangar with an A is the large building housing aircraft. A hanger with an E is what you hang clothes on, or anything that hangs. Aircraft take the A, and the two are unrelated despite the identical sound.
The core difference
A building and a hook.
- hangar — an aircraft shed: the plane was in the hangar.
- hanger — for hanging things: a coat hanger.
Where hangar came from
French, where it meant a shed or shelter — probably from a word for an enclosure. It entered English with aviation and has kept that narrow use, which is why the A spelling looks foreign: it is.
The compound to watch
Cliffhanger, paperhanger and hanger-on all take the E, because they are about hanging. Only the aircraft building takes the A.