George Carlin, the standup comic who defined the comedy counterculture and went to the Supreme Court to defend his “Seven Words You Can Never Say on Television” routine, died Sunday of heart failure at St. John’s Health Center in Santa Monica. He was 71.

The four-time Grammy winner had performed as recently as last weekend at the Orleans Casino and Hotel in Las Vegas.

Ben Stiller called Carlin “a hugely influential force in standup comedy. He had an amazing mind, and his humor was brave and always challenging us to look at ourselves and question our belief systems, while being incredibly entertaining. He was one of the greats.”

Carlin constantly breached the accepted boundaries of comedy and language, particularly with his routine on the “Seven Words” — all of which are taboo on broadcast TV to this day.

When he uttered all seven at a show in Milwaukee in 1972, he was arrested on charges of disturbing the peace, freed on $150 bail and exonerated when a Wisconsin judge dismissed the case, saying it was indecent but citing free speech and the lack of any disturbance.

When the words were later played on a New York radio station, they resulted in a 1978 Supreme Court ruling upholding the government’s authority to sanction stations for broadcasting offensive language during hours when children might be listening.

Despite his reputation as unapologetically irreverent, Carlin was a television staple through the decades, serving as host of the “Saturday Night Live” debut in 1975 — noting on his website that he was “loaded on cocaine all week long” — and appearing some 130 times on “The Tonight Show.”

“He paved the route for a lot of new comics today,” said Laugh Factory owner Jamie Masada. “He had the most brilliant mind and really cared for the other comics,” Masada added.

“More than a comedian, he was a social commentator whose legacy not only includes memorably funny monologues but also routines that spoke powerfully for freedom of expression,” said Neil Portnow, prexy-CEO of the Recording Academy.

“No performer was more important to helping our network define itself in its early years. And no performer was more committed to the ideal of freedom of speech, a principle he embodied for the 50 years he performed with his trademark wit,” said HBO in a statement.

“Why do they lock gas station bathrooms?” Carlin once mused. “Are they afraid someone will clean them?”

He produced 23 comedy albums, 14 HBO specials, three books and a few TV shows, and he appeared in several movies, from his own comedy specials to “Car Wash,” “Bill and Ted’s Excellent Adventure,” “The Prince of Tides” and Kevin Smith’s “Dogma.”

Carlin won four Grammy Awards for spoken comedy album and was nominated for five Emmys. Carlin was to be presented with the 11th annual Mark Twain Prize for American Humor on Nov. 10 in Washington, with the ceremony to be broadcast on PBS.

“Nobody was funnier than George Carlin,” said Judd Apatow. “I spent half my childhood in my room listening to his records experiencing pure joy. And he was as kind as he was funny.”

Carlin started his career on the traditional nightclub circuit in a coat and tie, pairing with Jack Burns to spoof TV gameshows, news and movies. Perhaps in spite of the outlaw soul, “George was fairly conservative when I met him,” said Burns, describing himself as the more left-leaning of the two. It was a degree of separation that would reverse when they came upon Lenny Bruce, the original shock comic, in the early ’60s.

“We were working in Chicago, and we went to see Lenny, and we were both blown away,” Burns said, recalling the moment as the beginning of the end for their collaboration (though not their close friendship). “It was an epiphany for George. The comedy we were doing at the time wasn’t exactly groundbreaking, and George knew then that he wanted to go in a different direction.”

The change would transform Carlin into more than a comedian: He also became a social commentator and philosopher — a position he would relish through the years.

Carlin often railed against the evils of religion, which caused controversy throughout his career. He felt the taboos against bad language and indecency were caused by religious superstition.

He battled drug and alcohol dependency over the years and suffered three previous heart attacks. In 2004, he was fired from the MGM Grand Hotel in Las Vegas after sparring with the audience during a dark routine filled with references to suicide bombings.

Carlin grew up in the Morningside Heights section of Manhattan, raised by a single mother. After dropping out of school in the ninth grade, he joined the Air Force in 1954. He received three courts-martial and numerous disciplinary punishments, according to his official website.

While in the Air Force he started working as an off-base disc jockey at a radio station in Shreveport, La., and after receiving a general discharge in 1957, took an announcing job at WEZE in Boston.

From there he went on to a job on the night shift as a deejay at a radio station in Fort Worth, Texas. Carlin also worked a variety of temporary jobs, including carnival organist and marketing director for peanut brittle.

In 1960, he left with $300 and Burns, a Texas radio buddy, for Hollywood to pursue a nightclub career as comedy team Burns & Carlin. His first break came just months later when the duo appeared on Jack Paar’s “Tonight Show.”

“I was doing superficial comedy entertaining people who didn’t really care: businessmen, people in nightclubs, conservative people. And I had been doing that for the better part of 10 years when it finally dawned on me that I was in the wrong place doing the wrong things for the wrong people,” Carlin reflected recently as he prepared for his 14th HBO special, “It’s Bad for Ya.”

Eventually Carlin ditched the buttoned-up look for his trademark beard, ponytail and all-black attire.

But even with his decidedly adult-comedy bent, Carlin never lost his childlike sense of mischief, even providing his voice to kid-friendly projects like the TV show “Thomas the Tank Engine and Friends” and the 2006 Pixar hit “Cars.”

Carlin’s first wife, Brenda, died in 1997. He is survived by wife Sally Wade; a daughter; and a brother.