The first use of this expression, and its likely origin, is by the writer Miguel de Cervantes, in his story Don Quixote de la Mancha written from 1605-1615. According to Bartlett's, the expression 'As well look for as needle in a bottle of hay' (translated from the original Spanish) appears in part III, chapter 10. 'Bottle' is an old word for a bundle of hay, taken from the French word botte, meaning bundle. Brewer (1870-94 dictionary and revisions) lists the full expression - 'looking for a needle in a bottle of hay' which tells us that the term was first used in this form, and was later adapted during the 1900s into the modern form.
And that modern use is - impossible search for something relatively tiny, lost or hidden in something that is relatively enormous.
I can't locate a clip of it but years back on The Amazing Race there was a needle in the haystack challenge and one team spent 8+ hours looking for it and never did locate it. Phil has to come out to where they were for the challenge to tell them they had been eliminated. It was AWFUL! The one team member who had to do the looking had cuts and scratches from the hay, she was bleeding. It was so frustrating to watch her try to find it - imagine how she felt as the one having to search!