Why are there generic drugs, surely all drugs are patented by those who develop them? Patents for drugs generally last for 20 years however they are taken out early in the development process and with lengthy animal then human clinical trials it can take a decade or more from getting the patent to going into production. Other companies may challenge patents or get around them by producing drugs which are do the same job but different enough not to be affected by existing patents. Companies may also produce generic versions of a patented drug in countries not covered by the patent. Companies can apply for a 5-year extension to their patent to make up for the time they were going through the approval process.
They cannot however patent the active ingredient for more than the length of the patent or renew patents that have expired, though they can patent the inactive ingredients and specific forms of compounds used.
Companies protect their product by patenting aspects of their drug such as the size shape and colour of the pill as well as the brand name, these patents do not run out.
There is also what is known as a research exemption to these patents, other companies can research and develop their own generic versions of the drug whilst the patent protection is still in place and so can move fast to get their product onto the market once the patent expires.
The first company to file an accepted abbreviated new drug application (for a generic) can also gain a 180 day exclusivity to produce a sell their generic version, this can be applied for by the original innovator company as a way of prolonging their exclusivity, though not using the original drug as there have to be some differences to make it a generic.