He was born at Akeds Road, Halifax, the only son of James Stansfeld (1792–1872), originally a member of a firm of solicitors, Stansfeld & Craven, and subsequently county-court judge in the district. His mother was Emma, daughter of John Ralph, minister of the Northgate-End Unitarian chapel, Halifax, and his sister married George Dixon. Brought up as a nonconformist, Stansfeld was in 1837 sent to University College, London, and graduated B.A. in 1840 and LL.B. in 1844. He was admitted a student of the Middle Temple on 31 October 1840, and was called to the bar on 26 January 1849; he does not seem, however, to have practised as a barrister, and later in life derived his income mainly from a brewery at Fulham.
On 27 July 1844 Stansfeld married Caroline, second daughter of William Henry Ashurst, a radical and friend of Giuseppe Mazzini, to whom Stansfeld was introduced in 1847: they became close. Stansfeld also sympathised with the Chartist movement, even if Feargus O'Connor denounced him. He also took an active part in propagating radical opinions in the north of England, frequently spoke at meetings of the Northern Reform Union, and was one of the promoters of the association for the repeal of "taxes on knowledge".
In 1859, Stansfeld was returned to Parliament as Radical member for Halifax, which he continued to represent for over thirty-six years. He voted consistently on the Radical side, but his chief energies were devoted to promoting the cause of Italian unity. He was selected by Giuseppe Garibaldi as his adviser when the Italian patriot visited England in 1862. In 1863, he moved in the House of Commons a resolution of sympathy with the Poles, and two months later was made Civil Lord of the Admiralty. In 1864, as the result of charges made against him by the French authorities, in connection with Greco's conspiracy against Napoleon III, Disraeli, in the House of Commons, accused him of "being in correspondence with the assassins of Europe."
Stansfeld was vigorously defended by John Bright and William Edward Forster, and his explanation was accepted as quite satisfactory by Palmerston. Nevertheless, he only escaped a vote of censure by ten votes, and accordingly resigned office. In 1865, he was re-elected for Halifax, and in 1866 became Under-Secretary of State for India under Lord Russell. He served in the first William Gladstone administration of 1868 to 1874 as a Lord of the Treasury between 1868 and 1869, as Financial Secretary to the Treasury between 1869 and 1871 and as President of the Poor Law Board (with a seat in the cabinet) in 1871, before being appointed the first President of the Local Government Board, in 1871, a post he held until the Liberals lost power in 1874. He was sworn of the Privy Council in 1869.
The remainder of his life was mainly spent in endeavouring to secure the repeal of the Contagious Diseases Acts, and in 1886 this object was attained. He did not serve in Gladstone's 1880 to 1885 administration, but returned to the government in April 1886, when he again became President of the Local Government Board under Gladstone. However, the government fell in July of the same year.
He died at his residence, Castle Hill, Rotherfield, Sussex, on 17 February 1898, and was buried at Rotherfield on the 22nd, aged 77.