Technically it is impossible short of verifying every circuit path, bit of storage/memory and piece of data on your computer.
You have to practically start your trust some where. Computers are incredibly complex devices and there are simply too many possible attack vectors.
For the majority of attacks, formatting the hard drives and doing a clean install is sufficient, however there are demonstrated attacks that can compromise systems permanently or infect software you build yourself through hardware or compiler level exploits.
If you expect you are targeted, you're best bet is to source hardware from someplace you trust and be very careful what you install. If you just want to be safe from general threats, avoid questionable content, patch your system regularly and keep virus scanner up to date to minimize risk of an infection after you rebuild.