To quote Heinlein's obituary:
To the end Heinlein retained the libertarian notions on which he had been brought up, and believed that governments had no business to be meddling in the lives of individuals. Paradoxically, perhaps, he held the discipline of military life in some awe, and in his fiction, at least, had little time for incompetence or self pity.
He was a somewhat left-wing in the thirties, partly influenced by H. G. Wells's socialist ideas. By the 1950s he had become disenchanted with socialism, and wrote the strongly conservative "Who Are the Heirs of Patrick Henry?". This shift culminated in the strongly anti-communist "Starship Troopers" in 1958. He participated in the Goldwater campaign in 1964.
The phrase TANSTAAFL comes from "The Moon is a Harsh Mistress," a blatantly anarchist novel that was sometimes described as libertarian, or as the "Atlas Shrugged of SciFi", which expressed his political mindset at the time.
Overall his change from Democrat to Goldwater Republican to libertarian did not represent serious change in his viewpoint; rather it reflected the way that parties shifted around him over the years. Many democrats of the 1930s found themslves republican by the 1950s because, as Reagan said of himself over the same period, "I didn't leave the democrats; the democrats left me." Like Heinlein, Reagan's and Goldwater's views were slanted heavily libertarian. (Reagan's actions in office were a mix of libertarian and statism.)
But to make a long story short, Heinlein was essentially libertarian.
To quote Robert Heinlein:
"There ain't no such thing as a free lunch." And isn't,' I added, pointing to a FREE LUNCH sign across room, 'or these drinks would cost half as much. Was reminding her that anything free costs twice as much in the long run or turns out worthless.'
If America was the free lunch you want it to be, that's how it would have turned out. Worthless.
TANSTAAFL