Ronaldo, who has been playing despite a left knee injury, sent in a cross in the fifth minute of stoppage time and Varela scored with a diving header in the last seconds of the match.
Nani had scored first for Portugal, shooting past a sprawling Tim Howard in the fifth minute, but the Americans responded in the second half as Portugal seemed to wilt in the stifling heat.
Jermaine Jones made it 1-1 with a curling shot in the 64th minute after a cross from Graham Zusi made its way through the Portugal defense.
Clint Dempsey, playing with a broken nose, then put the Americans ahead in the 81st. The United States captain used his stomach to direct the ball into the net from a cross by Zusi.
The last-second draw denied the Americans a spot in the second round, but it kept Portugal alive in the tournament.
The United States now has four points in Group G, the same as Germany. Both Portugal and Ghana have one point. The Americans will face Germany on Thursday in Recife, while Portugal takes on Ghana at the same time in Brasilia.