Brand and generic drugs have differnet names, dosage and price. Thus, when you buy generics, you pay less but get the very same quality.
A huge part of a brand drug price consists of research and development costs, and advertising. Drug companies that produce generic versions simply buy chemical ingredients and spend money on production. Thus they can sets lower prices on their products.