Hanna, great question!
I looked around a good bit for the etymology of this phrase before I decided to respond. Trouble is, it seems whereas most phrases common to the US can be explained, their history given, this one is a bit of a mystery. Most people seem to think it has to do with people talking on a train; I think that's a silly guess. I've got one I think is better. (But, it's still just my guess.)
The phrase fragment "get off" is often used to refer to escaping trouble or repercussion. I think that originally comes from the metaphor "to get off the hook" as a fish might (if caught on a hook), and then obviously would receive a similar reward of escape.
So my guess is that this phrase originated from somebody literally asking another person how they thought they might avoid trouble, when doing whatever action is in reference, which by inference would be an action that one would expect to bring trouble. "Where do you get off (the hook) calling the president a dunce?" (i.e., hook being that you would be in trouble for calling the president a dunce. Luckily that's not the case, hehe)
To make this clearer, there's long been a tendency in English to swap "where" and "how" even against the grain of grammar rules.
Man 1: "I just got a job in a big budget movie in Hollywood."
Man 2: "Where'd you get a sweet gig like that??"
(literal answer would be "Hollywood;" he's already said that. But Man 2 is switching "where" and "how" in this example. What he means is "How (did you manage) to get a sweet gig like that??" )
So I think the literal meaning of this phrase is "Where [/How] do you get off [the hook], __ing the ___ like that
Hope this helps. [I'll just say again I'm disappointed that 10 billion internet denizens all guessed that this had to do with a bus or train conversation. I'd bet money against that etymology.] =)