After attending Jefferson College, Canonsburg, Pennsylvania, and reading law under John Thomson Mason, Hamilton was admitted to the bar at Hagerstown in 1845.
In 1846 Hamilton was elected to the Maryland House of Delegates as a Democrat. Here he upheld the payment of interest on the state debt and staunchly supported the general financial policy of the Whig governor, Thomas George Pratt. Owing largely to this lack of machine regularity, he failed of reelection in 1847, but he regained his seat in 1848, although western Maryland was temporarily swept into the Whig line, and in 1849 he began a service as representative of Maryland in Congress which continued until March 3, 1855.
He conducted his campaigns for reelection by means of joint debates in which opposition to protective tariff was his principal argument and in 1852 won the election despite the fact that he was opposed by the veteran politician and debater, Francis Thomas. It was not until the Know-Nothing wave rose menacingly in 1854 that he was defeated. Upon returning to private life in Hagerstown, Hamilton devoted himself to his law practice, and to municipal interests, personal affairs, and farming. He was especially active in encouraging all improved methods in agriculture.
At the outbreak of the Civil War, like most influential Democrats in the border states, he found himself in a difficult position. He upheld the right of the South to secede although he deplored secession. His Southern sympathies were somewhat less pronounced than those of his law partner, Richard H. Alvey, but the two men were leaders in the so-called "Peace" party which was stigmatized by one of the local Unionist sympathizers as "Jeff Davis' masked battery. "
By 1867 the Democratic party had rallied in western Maryland, and in 1868 Hamilton was elected to the Senate, serving from 1869 until 1875. His activity was restricted during the radical Republican ascendancy. He favored rapid resumption of specie payments and upheld the bills for the admission of the Southern states and the resumption of home rule within them. Naturally, he voted against the Fifteenth Amendment. He strongly opposed the "salary grab, " and refused to profit by it. In his last term he was chairman of the committee for the District of Columbia and was instrumental in securing improvements in the water system of Washington.
A split in the Maryland Democratic machine developed by 1871 and soon grew acute. Hamilton, as the recognized leader of one faction, deplored the dominance of the Chesapeake Canal interests in Maryland politics and on the expiration of his term in the Senate devoted his energies to advocating honesty and economy in the state administration, and reform in political methods. He particularly favored civil-service regulation of state appointments.
In 1875 he failed to secure the nomination for governor although the convention was closely divided. In 1879, however, he carried the convention and was elected by an easy majority. His views regarding strict economy and political reform foredoomed him to an administration of trouble and friction. The state Senate was largely in the hands of the canal supporters and the treasury department was definitely controlled by his enemies. His message of 1882 was a masterly presentation of the measures necessary for a clean, efficient administration, but his appointments were blocked by the legislature and his measures were frequently rejected. From 1884 until his death he occupied himself with private matters and with the welfare of Hagerstown.
As a member of the House Hamilton supported orthodox Democratic measures, for he viewed the South as the victim of tyrannical Northern sectionalism.
In 1859 Hamilton married Clara Holmes Jenness, who with six children survived him.