17 years from the time the patent is filed. If perfecting the product (research and development) consumes more time, that time is subtracted from the 17 years.
In other words, say Electrolux patents a new heating element for a gas dryer in 2000. They don't have the product perfected until 2004. They lose the patent in 2017, but only 13 years after bringing the product on the market.
The same applies for new medications. A company may patent a drug but require 5-10 years to get the drug approved by the FDA in the US. If the approval process takes ten years after patent filing, the drug company gets only seven years to sell the product before generics can be produced.