A university patent office explained it to me as follows (note of course that doesn't guarantee this person was correct): patents do cover any "practice" of the patent, which includes practicing it for the purpose of even merely understanding and improving it. So research is not legally exempt. But in practice, patent infringement damages are almost exclusively at a court's discretion, and courts are almost exclusively interested in unlicensed commercial exploitation. A court can in principle issue a wide range of relief: an injunction against practicing the patent; a mandatory licensing fee; compensatory damages; or some combination of those. The nature of the relief is supposed to depend on the nature of the infringement and considerations of public policy. A widespread assumption is that it's not worth suing researchers who are trying to improve an invention without commercially exploiting the patent, because a court will be very unsympathetic to the patent holder, and unlikely to award any substantial relief.
Although I imagine being able to conduct research but never commercially exploit any discoveries made without the prospect of winding up in court would have a significant chilling effect on research funding.
As I understand it there's no "research exception" for patents, and they don't just apply to "selling". I'm basing my understanding on this article:
http://www.nature.com/nbt/journal/v25/n11/full/nbt1107-1225....
It's also true that researchers rarely take any notice of patents, and the issue just gets ignored.