Gayle scored 317 as the home side reached stumps on 565 for five in reply to South Africa's first innings of 588 for six declared. Shivnarine Chanderpaul was 82 not out with Dwayne Bravo on 10.
Before Gayle's epic knock, the highest innings against South Africa was Australian Donald Bradman's 299 not out, which he scored in Adelaide in 1931-32.
Gayle followed West Indian triple centurions Garfield Sobers, Lawrence Rowe and Brian Lara into the record books.
His innings was the fourth highest by a West Indian, and the 14th highest in test history.
Gayle bettered the 213 Wavell Hinds scored in the first test of the current series in Georgetown as the highest innings by a West Indian against South Africa.
His previous highest score was the 204 he made against New Zealand in Grenada in 2002.
The left-handed opener faced 483 balls and hit 34 fours and three sixes in an innings that he began aggressively before settling down to bat with calm authority.
South Africa squandered a second chance to dismiss Gayle when, on 298, he edged one of Graeme Smith's off-spinners and was dropped by wicketkeeper Mark Boucher.
Gayle was first dropped on 80 on Sunday, and resumed on 184 not out. He reached his triple century five balls before tea with a single off the bowling of occasional medium pacer AB de Villiers.
Gayle was dismissed in the seventh over after tea when he attempted to guide a delivery from fast bowler Monde Zondeki to third man and sent a head high catch to Smith at slip.
The West Indies resumed on 299 for one, and Gayle and Ramnaresh Sarwan took their partnership to a record 331 before Sarwan's slashed drive to a ball from Zondeki was caught low down by Ashwell Prince at backward point.
Sarwan scored a disciplined 127 off 279 balls with 14 fours and two sixes. His dismissal ended the biggest stand for any wicket that the West Indies have scored against South Africa.
Lara, whose 36th birthday on Monday was heralded in song by a section of the crowd, changed his gloves twice before getting off the mark with the 20th ball he faced.
But that proved to be Lara's only scoring shot, because nine balls later he sparred at another delivery from Zondeki and edged a catch to Boucher to be caught behind for four.
Lara has amassed world record scores of 400 not out and 375 at the Antigua Recreation Ground where the test is being played and the crowd expressed its disappointment that he was not able to add to the six centuries already scored in the match.
Gayle's efforts made up for that and 12 overs after the hero was dismissed Smith had Narsingh Deonarine caught by Boucher for four. Zondeki took three for 87 from 18 overs.
South Africa have an unassailable 2-0 lead in the four-match series.
South Africa 1st innings 588 for 6 decl (J. Kallis 147, A. Prince 131, G. Smith 126, A. de Villiers 114)
West Indies 1st innings (Overnight: 299-1)
C. Gayle c Smith b Zondeki 317
W. Hinds c&b Ntini 0
R. Sarwan c Prince b Zondeki 127
B. Lara c Boucher b Zondeki 4
S. Chanderpaul not out 82
N. Deonarine c Boucher b Smith 4
D. Bravo not out 10
Extras (lb-4 nb-15 w-2) 21
Total (for 5 wickets, 178 overs) 565
Fall of wickets: 1-14, 2-345, 3-363, 4-512, 5-535.
To bat: C. Browne, D. Powell, D. Washington, T. Best
Bowling Pollock 29 - 4 - 94 - 0 (nb-9) Ntini 28 - 2 - 94 - 1 (nb-2 w-1) Zondeki 18 - 2 - 87 - 3 (nb-2 w-1) Kallis 32 - 5 - 86 - 0 Boje 30 - 6 - 76 - 0 Smith 29 - 2 - 98 - 1 (nb-2) AB de Villiers 5 - 1 - 6 - 0 Prince 7 - 0 - 20 - 0