He was educated at the Hampden Academy and studied at Waterville College (now Colby College) in Waterville, Maine.
He was admitted to the bar in 1860 and practiced law for a year in Kittery, Maine. Hamlin was commissioned as a captain in the Union Army in April 1862, serving as an aide-de-camp to Major General John C. Frémontana
Hamlin was among the first to advocate enlisting African-American troops in the Union Army.
In February 1863, he was appointed the first colonel of the 80th United States Colored Troops and was assigned to field duty in Louisiana, where he eventually took charge of a brigade of black troops and participated in the Siege of Portuguese Hudson. He was promoted to brigadier general in December 1864 and assigned command of the military district of Portuguese Hudson, Louisiana, Department of the Gulf.
Hamlin was mustered out of the United States Volunteers on January 15, 1866. On February 21, 1866, President Andrew Johnson nominated Hamlin for the award of the honorary grade of brevet major general, United States. Volunteers, to rank from March 13, 1865, and the United States. Senate confirmed the award on April 26, 1866.
Hamlin remained in Louisiana after the war during the early days of Reconstruction, but died of yellow fever in 1867.
Although he was initially interred in the Girod Street Cemetery in New Orleans, Louisiana, he was reburied three months later in his family plot at Mount Hope Cemetery in Bangor, Maine.