EAST RUTHERFORD, N.J.: Mexico earned chance after chance to score against Costa Rica.

The goal that finally came might not have been so well-deserved.

Mexico won 1-0 on Sunday night in the CONCACAF Gold Cup quarterfinals on Andres Guardado's penalty kick in the final minute of extra time. Roy Miller had been whistled for a questionable foul on Oribe Peralta with the game seconds from both teams attempting penalty kicks.

Guardado converted to send six-time champion Mexico to the semifinals, where it will face Panama on Wednesday night in Atlanta.

Mexico had dominated play but couldn't put the ball into the net until the final moments, and it took the penalty kick. Angry Costa Rica fans — heavily outnumbered by Mexico's supporters in the crowd of 74,187 at MetLife Stadium — threw bottles toward the field in protest.

With Peralta leaping to try to head in a cross in front of the goal in the second minute of stoppage time, Miller appeared to make little contact with him. But assistant referee Eric Boria of the United States called it a foul.

Costa Rica coach Paulo Wanchope declined to criticize the ruling, saying, "That's football."

"We shouldn't take away from Mexico," he said. "They played a good game. They had their chances."

The Mexico-Costa Rica matchup looked like a potential final at the start of the tournament, with Costa Rica coming off a World Cup quarterfinal appearance last year. But both teams were sluggish in the group stage to each finish second and set up the quarterfinal meeting.

Costa Rica lost in the quarterfinals for the third straight Gold Cup.

Injury-plagued Mexico repeatedly broke down Costa Rica's defense to set up good looks only for shots to sail high or wide.

"That has been our main concern since the beginning of the tournament, to be effective up front," Mexico coach Miguel Herrera said. "Unfortunately we haven't been able to be as effective as we would want to."

Carlos Esquivel nearly put Mexico ahead in the 63th minute, but goalkeeper Esteban Alvarado got his fingertips on the ball while leaning the other way to deflect it off the crossbar. Alvarado had five saves.

"You have only a certain number of plays to win a game before you start suffering, and you start thinking that if you give your opponent a break, you lose," Herrera said.

Panama also went to extra time in the first game Sunday against Trinidad and Tobago. That game made it to penalty kicks with the score tied 1-1, and Panama won 6-5.

Jaime Penedo made the clinching save for the 6-5 victory.

Penedo saved Lester Peltier's shot after Trinidad and Tobago had a chance to win it in the seventh round. But Daniel Cyrus' try sailed over the crossbar to keep Panama alive.

Luis Tejada scored in the 37th minute for Panama, and Kenwyne Jones tied it the 54th for Trinidad and Tobago.

In each of its four Gold Cup games, Panama has gone up 1-0 only to allow the equalizer in the second half. But Panama, the runner-up in the 2013 Gold Cup, keeps advancing and will face the winner of Sunday's second match at MetLife Stadium between Mexico and Costa Rica.

"Panama played one of its best matches and I want people in Panama to start thinking and analyzing matches not only for the result, but for the way it plays," coach Hernan Dario Gomez said.

Panama controlled possession on a steamy Sunday but couldn't put away its opportunities. Panama had 13 corner kicks, while Trinidad and Tobago didn't have a single one. But goalkeeper Marvin Phillip made five saves to keep Trinidad and Tobago in it.

Blas Perez had a chance to put Panama in position to win in regulation when Erick Davis launched a perfectly placed cross to find him unmarked on the far post during second-half stoppage time. But Perez's volley sailed across the goal and just wide.

"We've lacked calmness to convert the scoring opportunities we've had in matches," Gomez said.

The lone goal came when Sheldon Bateau's attempted clearance in the box ricocheted off Perez and right to Tejada, who was alone in front of the goal. He beat Phillip with a low shot for his 10th career Gold Cup goal.