However, they then ran out of ideas and by the time Jakub Rzezniczak scored in stoppage time it was too late.
Legia’s distraught players laid on the pitch for several minutes after the final whistle having blown a golden chance to qualify for the first time since 1995.
In London, Aaron Ramsey scored in each half as Arsenal finished off outclassed Fenerbahce.
Ramsey swept the ball home in the 25th minute after Caner Erkin’s attempted tackle on Theo Walcott pushed the ball into his path.
The Wales midfielder then finished off a sweeping move with a delicate side-footed volley into the far corner in the 72nd.
Basel’s Fabian Frei and Philipp Degen each scored as the Swiss side qualified for the fourth time.