Bafana's goals came in the second half, from super-sub Jabulani Shongwe and Hlompho Kekana, before Willy Stephanus got one back for the Brave Warriors.
The match started slowly, with neither side able to hold on to possession – a situation excacerbated by the poor condition of the pitch.
Namibia took control of the ball and had the first real chance of the match on 17 minutes, when Jakob Pinehas tried a shot, but Wayne Sandilands made a good save low to his left.
Shortly after that Bafana, with six new caps making debuts, came into the match, and Edward Manqele had a chance on goal, but his shot was woefully wide.
Thulani Hlatshwayo had a shot from the edge of the box on 25 minutes well saved by Virgil Vries, before Lerato Chabangu tried a powerful shot from range, which Vries tipped over the bar.
Gordon Igesund brought on Shongwe for the start of the second half and the Mamelodi Sundowns youngster made an immediate impact, scoring with his second touch of the game to hand Bafana the lead.
And what a goal it was, a swerving rocket from 25 yards that Vries completely misjudged.
Shongwe’s Sundowns teammate Kekana made it 2-0 with a shot from inside the box on 62 minutes, after being sublimely set up by some skill by Chabangu.
In the 70th minute the referee adjudged Buhle Mkhwanazi to have handled the ball in the box, and awarded Namibia a penalty.
Willy Stephanus’ first attempt from the spot was saved by Sandilands, but the referee ordered a retake, adjudging the goalkeeper to have moved before the penalty was taken.
Stephanus hit the second attempt into the top corner of goal to make it 2-1.
Bafana held on for the win despite a tense last 15 minutes.
South Africa now await the identity of their semi-final opponent, which will be either Mozambique or hosts Zambia, who play on Sunday in Kitwe.
“We knew Namibia would press us but the players worked as a unit. We showed character after only being together for the last two days,” man of the match Ruzaigh Gamildien said after the game.
South Africa line-up: Wayne Sandilands (c), Tshepo Gumede, Sibusiso Khumalo (Ruzaigh Gamildien 34), Thulani Hlatshwayo, Hlompho Kekana, Sibusiso Msomi (Jabulani Shongwe 45), Buhle Mkhwanazi, Lerato Chabangu (Katlego Mashego 76), Lebogang Manyama, Edward Manqele, Mzikayise Mashaba
Bench: Gladwin Shitolo, Katlego Mashego, Mandla Masango, Ruzaigh Gamildien, Jabulani Shongwe, Siyabonga Mpontshane, Thabani Mthembu, Lehlohonolo Nonyane
Namibia line-up: Virgil Vries, Angula da Costa (c), Willem Mwedihanga, Larry Horaeb, Denzil Hoaseb, Ananias Gebhardt (Deon Hotto 56), Freedom Puriza, Petrus Shitembi, Neville Tjiueza (Willy Stephanus 55), Jakob Pinehas (Harold Ochurub 75), Sydney Urikhob
Bench: Efriam Tjihonge, Chris Katjiukua, Emilio Martin, Deon Hotto, Willy Stephanus, Harold Ochurub, Roger Katjiteo, Riaan Cloete, Denis Tjetjinda