Eli5: how do industrial farms prevent blemished or inedible products from reaching the store?

My mom recently bought a big box of potatoes from a small farm owner and 25% of them were partially green. Apparently they’re not safe to eat?A pear she also bought had a LIVING worm inside it when I bit into it.

I’ve never experienced this with store bought products so it got me wondering, how do the industrial farms keep product quality so high? What kind of machinery do they use to sort bad stuff out? What exactly happens from harvest to packaging?

Sorry if flair is wrong.