Sociedad trailed 1-0 after the first leg and a 2-2 draw in San Sebastian meant they were beaten 3-2, while Moyes was dismissed for haranguing the officials 10 minutes from time.
Villarreal had defender Mateo Musacchio sent off with eight minutes left of an incident-packed encounter but they held on to secure a place in the last eight.
Moyes??? team found themselves 2-0 down on aggregate in the 26th minute when Giovani dos Santos found Gerard Moreno, who produced a fine finish, cutting inside and curling a left-footed shot into the corner of the net.
However, it was 1-1 on the night just before half-time when Carlos Vela scored from close range on the rebound after Sergio Asenjo did well to keep out an Imanol Agirretxe header.
But former Tottenham man Dos Santos looked to have wrapped things up for Villarreal with 17 minutes left when the ball fell kindly for him following a throw-in and he surged into the box before shooting home.
However, the hosts responded again moments later with a superb goal, Esteban Granero exchanging passes with Vela before finding the net with a powerful volley.
And former Manchester United boss Moyes was then sent to the stands after he was infuriated by a decision and seemingly suggested to the referee???s assistant that he should don a pair glasses.
Moyes took the decision in good grace, cheerfully hurdling a barrier to join the fans and then accepting a crisp offered by a young supporter.
Musacchio was then dismissed when he received a second yellow card for a foul on Agirretxe on the edge of the box but Real could not capitalise on the free-kick and were then kept at bay in the closing stages.
Villarreal will next face Getafe, who beat Almeria 2-1 on aggregate thanks to an Alvaro Vazquez goal.
And Athletic Bilbao sneaked into the quarter-finals on away goals despite being defeated 2-0 at home by Celta Vigo.