Ruiz headed powerfully home a minute before halftime to leave Costa Rica fans dancing in the sunshine at Recife's Pernambuco arena. (Agencies)
"We kept up our heads. It was a beautiful match... The people of Costa Rica deserve this. They supported the team and me. This was for them," said ecstatic coach Jorge Luis Pinto.
Costa Rica top the group with six points, having already beaten Uruguay in their first game, while Italy, three points adrift, now need at least a draw from their final game against Uruguay, also on three points, to go through on goal difference.
"We knew this would be a particularly tough group. We will try to recover all our energy for the Uruguay game," said Italy coach Cesare Prandelli,” he said.
"We didn't get our tactics wrong. They were very, very aggressive and we were trying to find different ways into the game but we didn't manage it," he added.
In making the last 16 for the first time in 24 years, the Central Americans ensured England became the latest team to make a surprise early exit, leaving at the group stages for the first time since 1958.
The midday sun reduced the early stages to shadow boxing, with Italy hogging the ball but strolling round the pitch at walking pace while Costa Rica were busy but cautious.
Costa Rica's back-five pushed high up the pitch to compress the space as Italy sat back and looked to hit on the break, a tactic which almost paid dividends on the half-hour mark as Mario Balotelli was sent clear by a superb through ball from Andrea Pirlo.
But the striker, who headed the winner in Italy's opening win over England, miscontrolled the ball with his first touch and then tried and failed to lob keeper Kaylor Nevas with his second.
As the first half was drawing to a close, Costa Rica showed why they can pose such a threat.
In the space of three minutes, Oscar Duarte headed fractionally over, Joel Campbell had a penalty appeal waved away by Chilean referee Enrique Osses and Ruiz headed them in front.
Junior Diaz floated a beautiful cross to the far post and Ruiz arrived to head it in off the underside of the bar and over the line, with the decision to award a goal confirmed on the big screen.
Italy brought on Antonio Cassano at halftime, but Costa Rica expertly took the sting out of the match, reducing their opponents only to glimpses of goal, while they continued to look threatening on the counter.
Ruiz headed powerfully home a minute before halftime to leave Costa Rica fans dancing in the sunshine at Recife's Pernambuco arena.