@AlekStorm's answer is pretty good, but I'd say that in practice there are at least two ways linguists decide which form is underlying.
1) If you take two allophones, say (using relatively broad transcription) [t] and [ɾ]. You could, rather tediously, list all the contexts where [t] shows up, and all the contexts where [ɾ] shows up. Then, you can so some set operations. Call T the set where [t] shows up and D the context where [ɾ] shows up. It should be the case that T-D is a relatively small set of contexts, and D-T should be empty. In this case, [ɾ] has a restricted distribution, so the features of [t] could be considered underlying. This is similar to the elegance of description from @AlekStom's answer. Of course, in practice it will happen that neither A-B nor B-A is empty, and then everything gets more complicated.
2) You can check to see if one allophone "behaves" as if it were the other. As with [t] and [ɾ], the diagnostic would be
- Does [t] ever behave as if it were a voiced, lenis segment?
- Does [ɾ] ever behave as if it were a voiceless segment?
In fact, in many dialects [ɾ] does frequently behave as a voiceless segment, triggering changes in quality of its preceding vowel. For example, in Canadian English and some other dialects, there is the following distinction between write and ride.
raɪd (ride)
rʌɪt (write)
When adding on the morpheme -er, the /t/ in writer is flapped.
raɪɾɚ (rider)
rʌɪɾɚ (writer)
This case has the additional complication that both the [d] and the [t] are neutralized to [ɾ]. However, you could still argue that the vowel difference between [aɪ] and [ʌi] is triggered by voicelessness, so that the underlying form of the [ɾ] in writer is behaving as a voiceless segment.
But, like I said, heuristics like these are not applicable to all analyses, and there are certainly underlying forms which have been posited on flimsy evidence. And thank god, because that means that linguists' work is not done!