A lens that is made from low quality glass (or plastic!) will simply take less good pictures than a camera with a great lens. High quality lenses can take sharper pictures up close and far away.
Still, megapixels are not at all irrelevant. They determine how large images your cameras image sensor can save. Image sensors can also be in different sizes but let’s not touch that now. A 8 megapixel camera can take photos up to 3456 time 2304 pixels. That’s more than enough to print your photo on a 24 times 15 inch poster!
Megapixel count is still broadcasted as a competitor edge for mobile phone cameras and cheap compact cameras. The truth is that these cameras have small portable optics that work fine for your party photos or your vacation photos. Your images will print fine in smaller formats and will look good on web pages, but not on high quality printing products.
There are of course many more aspects that will put a DSLR above the smaller cameras, like aperture, shutter, ISO sensitivity and more. Those will not be covered here. The main lesson learned is: Don’t let the megapixels fool you! :)
Have a nice day.