The name has a rather unfortunate origin with the "Jim Crow laws" (named after a "minstrel" character) that enforced segregation in the Southern USA after The American Civil War. Though they were designed to prevent newly freed slaves from voting, the laws couldn't be written to say simply that "Black people can't vote." Thus, state legislatures enacted "poll taxes" (not a poll tax in the traditional sense of a fixed quantity everyone pays each year,note but a literal one: if you go to the polls and can't pay, you can't vote), which disqualified most black voters because they were almost all too poor, and "literacy tests," which disqualified most black voters because they were illiterate. Not that literacy would have helped them, since these "tests" were also arbitrarily difficult (with questions like "Name all the county judges in the state,"note or "Name the exact date Oklahoma was admitted to the Union,"note or "How many bubbles are there in a bar of soap?"note ) and had completely subjective scoring: If a white man took the test and got five out of ten, he could pass. If a black man took the test and got ten out of ten, he could be made to take it again. In French.
However, many white farmers were also poor and illiterate, meaning the Jim Crow laws would have affected them as well. In response, the legislatures changed the laws such that they had the effect of guaranteeing that any mannote whose grandfather had been able to vote — or would have been able to vote, had he lived in the state — could himself vote without paying the tax or proving literacy. As the grandfathers of most black farmers were slaves and thus unable to vote, while almost all the grandfathers of white farmers were citizens and able to vote, this served as an effective measure for disenfranchising African Americans without hurting poor whites. However, sometimes the laws were worded in such a way as also to discriminate against poor whites, especially in places where and times when they were hostile to the local Democratic Party. At some times more white men than black men were disenfranchised by laws ostensibly targeted at African Americans. These kinds of grandfather clauses were found unconstitutional in 1915; the rigged tests and taxes stayed until the Civil Rights Movement.
In more recent times, a grandfather clause can come into effect when any sort of laws are altered or updated, so that existing buildings, tenants or procedures are not affected. For example, when a realty company changes its regulations regarding pets, it's common to allow anyone who already had one to keep them, and simply disallow new pets.