Modern philosophy of language actually says a fair bit about this. (I've seen this example, and ones like it, in a number of philosophy articles. I'll give some references at the bottom.)
First, I'll argue that Lincoln is right. Then I'll say some stuff about the use/mention distinction, which is a central concept in the philosophy of language, and is key to seeing why Lincoln is right.
Imagine if everybody used the word "leg" (as in, the string of letters) to refer to what we call tails as well as what we call legs. That is, in this hypothetical scenario, "leg" means what we mean by "leg or tail".
Because this word means something different in this situation, it turns out that people speak a very slightly different language than English. Let's call their language English*. We can give give easy instructions on how to translate between English and English*: "leg" in English* means the same as "leg or tail" in English (and everything else means the same in English and English*).
Now, suppose we ask the question: In such a situation, how many legs does a dog have? What language are we speaking when we ask it? If we're speaking English, then the answer is "4" -- in the hypothetical scenario, dogs still have 4 legs (imagine it, if you like, there are lots of people going around pointing at dogs' tails saying "that's a leg", but the dogs still only have 4 legs).
If we're speaking English*, then the answer is 5, since, in English*, the sentence "dogs have 5 legs" is true. (Since, by our translation rule, it translates into the English sentence "dogs have 5 legs-or-tails".
But we are surely speaking English when we ask the question. Lincoln certainly was. In fact, in general, when evaluating counterfactuals, we're still speaking our language, rather than whatever language is around in the counterfactual. Imagine that we ask "if the English language had never existed (and everybody spoke French) how many legs would a dog have?". If we were supposed to evaluate the question in the language of the counterfactual situation, we would only be able to return a blank look, but we can answer "four". Similarly, we might want to answer counterfactual questions in which there are no people or languages.
(As an aside, the same goes with different times, as well as different counterfactual scenarios. So, we might ask whether whales ever used to be fish. The answer is no. People used to use the word "fish" to refer to whales, but that doesn't mean that whales are fish.)
There is a question in the vicinity of Lincoln's, which does have the answer "five". Compare the two following statements:
If the word "leg" applied to dogs' tails, then dogs would have had five legs.
If the word "leg" applied to dogs' tails, then "dogs have five legs" would have been true.
(1) is false, for the reasons that I've just said. It uses the word "leg", and we know that the word (in English) does not refer to tails. The second mentions the sentence, and asks whether it's true.
Use and mention
I've been using lots of quote marks in what I've just written, and I've been careful about where I've used them. This is because I want to be very careful to distinguish between where I've used a word, and where I've mentioned the word. Using a word means just that. If I say "John is tall", then I'm using the word "John" to say something about John. But if I say " "John" is four letters long", I'm talking about the word "John" (since John himself is obviously not four letters long). This is called mentioning the word "John" It is common in philosophy to use quotation marks to pick out a mention of a word, rather than a use.
(The Wikipedia article on the use/mention distinction is fairly good, apart from bizarrely citing the Strunk and White, who should never be consulted for style or grammar advice.)
What's this got to do with the question? Well, when we're using the word "leg", we're using it in our context, to mean leg. And as we use the word "leg", it does not refer to tails. To get at the thrust of what things are like in the hypothetical community, we need to mention the word "leg", to say that, for example, in the community it refers to tails, or that in the community, the sentence "dogs have 5 legs" is true. Notice that the antecedent of Lincoln's counterfactual is mentioning "leg" (despite it not being made clear with quotation marks).
References to philosophy
I promised some references for where I'd seen this recently. Here are a couple (not exhaustive, by all means)
Eli Hirsch uses this example a lot when discussing a view he calls "quantifier variance". He want to say that we could have used the word "exist" so that the sentence "Tables do not exist" is true. Nonetheless, he claims, we do not use the word in that way, and the sentence "tables exist" in in fact true, that is, tables exist.
Unfortunately, I can't find anything by him on this which isn't behind a paywall. On the offchance that you have access (maybe you or somebody you know has internet access from a university), here are some papers:
Issues related to this have been raised as objections to a form of deflationism about truth. Deflationists say that all there is to truth is something called the T-schema. This says:
"S" is true if and only if S
where S is some sentence. For example:
"Snow is white" is true if and only if snow is white
But there's a bit of a problem, since, as the Lincoln example shows, these come apart where we consider counterfactual situations in which the meanings of words change. Had we used "leg" to refer to dogs' tails, then "dogs have five legs" would have been true, but dogs would not have had five legs.