There's no general recipe. If your system has been infected by an unknown trojan, all you can do is reinstall.
If you know that the trojan operates in a certain way — for example you know that the trojan doesn't infect the kernel — there may be a less harsh way of recovering. But that's entirely dependent on knowing how the trojan behaves. If all you have is the symptoms (such as your computer sending spam without your consent), there's no general technique: the trojan detector has to be smarter than the trojan designer (and lucky). As far as trojans are concerned, detection and hiding are like gun and armor: there is a technological escalation, and neither party has an intrinsic advantage (though hiders do have a head start).
Many systems have a secure distribution channel in place. For example, when you install a package from the Ubuntu repositories with the apt-based tools (apt-get, aptitude, synaptic, software center, …), the tool checks that the package is signed (vetted) by someone Ubuntu trusts. (Most distributions have a similar mechanism.) When you install a package from a PPA, all you can know is that the PPA owner vetted the package, which is no help if you have no reason to trust the PPA owner in the first place.
About trojans and backdoors, I strongly recommend reading Ken Thompson's Turing award lecture, Reflections on Trusting Trust. To summarize, he changed the compiler so that when compiling the login program, it would add code that allowed him to log in with a secret password; then he changed the compiler so that when it compiled itself, it would insert the code to add the backdoor; then he recompiled the whole system (in particular the login program and the compiler); finally he restored the compiler source to the original, unquestionable source. Again, read Ken Thompson's article; then you may also read David Wheeler's counterpoint, perhaps best apprehended through Bruce Schneier's blog article.