Portugal were unable to find a way past a disciplined Slovenian backline, even though they threatened on a number of occasions at Deutsche Bank Park in Frankfurt.
Joao Palhinha struck the woodwork in first-half stoppage time, while Ronaldo failed to make the most of the four free-kicks he had in regulation time.
The Al-Nassr forward looked set to put that frustration behind them when he stepped up to the penalty spot in the 105th minute, only to be denied by the outstretched Jan Oblak.
However, it was Oblak's opposite number who had the defining say, with Costa making a crucial stop to deny Benjamin Sesko in the dying embers of extra time, before saving each of Slovenia's three penalties to claim a shootout victory, setting up a quarter-final clash with France.
Ronaldo left in tears
Roberto Martinez's side may have emerged victorious, but the game's most memorable moment took place in the 105th minute.
Ronaldo placed the ball down on the penalty spot after the referee Daniele Orsato ruled that Vanja Drkusic had fouled Diogo Jota as the Liverpool man drove into the box.
Sensationally, the 39-year-old saw his penalty expertly saved by Oblak, who dived to his left to push the ball onto the post and away from danger.
The miss left Ronaldo in tears at the break in extra time, but he ultimately regained his composure to score Portugal's first penalty in the subsequent shootout.
Costa proves to be Portugal's hero
Slovenia may have been frustrated to even be in a penalty shootout after Sesko spurned a glorious opportunity to win the game in the 115th minute.
The forward pounced on a mistake from Pepe to race into the box with only Costa to beat, but he was ultimately denied by the left leg of the Portuguese goalkeeper.
Not content with keeping his efforts in extra-time, Costa produced three more crucial saves in the penalty shootout to deny Josip Ilicic, Jure Balkovec and Benjamin Verbic.
While Costa kept Slovenia at bay, Ronaldo made amends for his earlier penalty miss, dispatching Portugal's first spot-kick before Bruno Fernandes and Bernardo Silva followed suit to send their country into the quarter-finals.
Do Portugal have a Ronaldo problem?
Ronaldo may have redeemed himself in the penalty shootout, but his overall performance was underwhelming and raises the question of whether he should still be starting for his country.
The veteran failed to find a breakthrough from any of his four free-kick attempts, with only one of those efforts troubling Oblak in the Slovenian goal.
Ronaldo still possesses the movement and nous of a top-level forward, but he offers very little out-of-possession and even the build-up phase of an attack.
While Martinez is unlikely to drop him for the quarter-final, there is surely a trail of thought that he could offer more as an impact substitute in the final 20 minutes rather than trying to affect the game from the start.
What next for Portugal and Slovenia?
Portugal have ultimately bettered their last-16 exit at Euro 2020, and they will now start preparations for Friday's last-eight clash against 2022 World Cup finalists France.
As for Slovenia, their first knockout round appearance at a major tournament may have ended in disappointment, but they can certainly be proud of their efforts as they now turn their focus to the start of the UEFA Nations League in September.