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.

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. That game made it to penalty kicks with the score tied 1-1, and Panama won 6-5.