After unsuccessfully standing for Chester in the 1673 by-election, Williams was elected Member of Parliament for the constituency in the 1675 by-election. His profile grew, and he was elected to become Speaker of the House of Commons, a post which he held during the 3rd and 4th parliaments of Charles II. He was the first Welsh Speaker. In June 1684, allegations were made against him that he had libelled the Duke of York for authorizing, as Speaker, the publication of Thomas Dangerfield's Information in 1680. Dangerfield, one of the most notorious of the Popish Plot informers, was by now utterly discredited. To provide the protection of a seat in parliament, Williams stood and was elected for Montgomeryshire in 1685; however his return was cancelled on petition, on the grounds that the contributory boroughs had no opportunity of voting. The prosecution resumed, and he was fined £10,000. He was also fined £20,000 after similar action was instigated by the Earl of Peterborough. Supporters worked on his behalf, including the Earl of Rochester; subsequently, £8,000 was accepted as full payment for the former fine, and Peterborough accepted a token payment for the latter after persuasion from James, now king. Previously a critic of James II, Williams entered the king's service in 1687, being appointed Solicitor General. He had been knighted two days previously, and in June 1688 he was created a baronet, of Gray's Inn in Middlesex. He held an important role in the prosecution of the Seven Bishops, but the violent antipathy between himself and the Lord Chief Justice, Sir Robert Wright, who accused him, with no relevance whatever to the issue before the Court, of taking bribes, probably contributed to the verdict of not guilty. Judge Jeffreys tried to ruin Williams with a fine for publishing a libel: this led to the pair engaging in a bitter personal and professional rivalry. He represented Beaumaris for the 1689 Convention Parliament, and turned against James after he fled England during the Glorious Revolution. He was placed on the committee appointed to draft the Bill of Rights. William III appointed Sir George Treby to succeed him as Solicitor General in the same year. Williams was made a King's Counsel and appointed Custos Rotulorum of Merionethshire and Denbighshire as consolation. The parliament declared the judgement against him for the publication of Dangerfield's Information illegal. He was not elected to Parliament in 1690, and prepared to stand again for Chester with Roger Whitley in 1695. Whitley was instead returned with Sir Thomas Grosvenor, and Williams was again returned for Beaumaris. He refused to take the new oath declaring William the rightful and lawful king, leading to his dismissal as King's Counsel. He left parliament in 1698.
Personal life
Williams married Margaret Kyffin on 14 April 1664, and they had four sons and one daughter. Williams died at his Gray's Inn chambers in 1700 and was buried at Llansilin in Wales. His baronetcy passed to his son William.