Fortunately for us, in North America we do not need to worry about viral contamination. So that leaves us generally with bacterial or chemical. I can't speak to chemical, but I can speak to bacterial.
The BEST method is boiling. It solves your problem in a straightforward and simple manner and purifies the water in your container.
FILTRATION: I personally used a Sawyer Trailshot filter last week to filter 4 liters out of a stagnant mud puddle. (Yes, I was that badly dehydrated and that little filter saved my bacon). These filters use mechanical filtration to exclude bacteria sized objects from passing through a membrane (or membranes).
Now a VERY important point about any filtration method is that it is very, very easy to cross contaminate and void all your filtration efforts. Think about getting your hands wet in dirty water, and then allowing a drop of that water to touch your clean outlet from your filter or enter your clean water and BAM, all the work is wasted. Having a disciplined methodology that YOU HAVE PRACTICED is just as important as the filter.
Carrying a small bottle of alcohol hand sanitizer can help here. Collect the contaminated water in a dedicated 'dirty' bottle, then sanitize, then filter from that container to a clean container.
PURIFICATION:
In addition to the filter, I carried 2 very tiny bottles of iodine tabs and an iodine neutralizer. The iodine tabs allow you to sterilize water in a container (and the container itself). After the iodine has done its work you use the neutralizer to get rid of the taste.
I used the iodine in my hydration bladder after I touched the inside of the bladder with the filter body just for insurance.
Steri-pens were mentioned previously. They use UV light to render bacteria sterile (they are no longer able to reproduce). The downside to them is that they only are reliable in clear water. If bacteria is shadowed by any particulate from the UV, it won't be effected.
Personally, boiling is the best option AFAIK, but for portability, that sawyer filter combined with the iodine was bullet proof. However, I did speak with a very experienced backpacker who said those sawyer filters were great until they wore out, at which point they "went downhill very quickly".