The problem is that today we have a scene where there appears to be a difference between the two drinks, but nobody can agree what that difference is. Is it that stouts contain roasted barley? Is it that porters are lighter, or sweeter? Is it that stouts have patent malt in them, while porters have chocolate malt?
Historically the answer is "none of the above," and most especially it's not true that stouts have to contain roasted barley, or patent malt, since stout was being brewed when roasted barley was an illegal ingredient and before patent malt was invented.
If we go back to the latter half of the 18th century and the first part of the 19th century, stout, more particularly brown stout, was simply the name for the strongest version of porter. Here's a quote from a book called "A General Dictionary of Commerce, Trade and Manufactures," published in 1810:
"Porter may be divided into two classes, namely brown-stout and porter properly so called … Brown-stout is only a fuller-bodied kind of porter than that which serves for ordinary drinking. A great deal of this is exported to America and the West Indies."
That "brown stout" simply meant "strong porter" is confirmed by a court case reported in The Times of London in 1803 over the theft of the contents of a cask of "porter, of superior quality, called Brown Stout" on its way from London to Barnsley, in Yorkshire. In court the stolen beer was described as both "remarkably fine old porter and very strong" and "excellent brown stout." Research by the beer historian Ron Pattinson among the records of London brewers in the early 19th century has shown that early 19th century brown stouts very often had identical recipes to the same brewer's porter, differing only in the amount of wort drawn off a given quantity of malt: less water was used for mashing stouts, so that they would be stronger.
Strong porter was called "brown stout" because it was still possible to find pale stout: "stout," when applied to beer, originally just meant "strong" (and the opposite word, for weak beer, was, at least occasionally, "slender"). Barclay Perkins of the Anchor brewery in Southwark, one of London's biggest porter brewers, was still brewing pale stout in 1805, made from 100 per cent pale malt, at a strength of around 7.9 per cent alcohol by volume. A brewer’s manual published around 1840 called "Every Man His Own Brewer," still referred to “stout ales” meaning strong ales in general. In 1843, Beamish and Crawford of the Cork Porter Brewery in Ireland began advertising "Bavarian Pale Stout", manufactured "under principles personally explained by Professor Liebig [a German scientist famous, at the time, for his studies of fermentation] to the manufacturers."
Patent malt, when it arrived in 1817, was specifically hailed as a way of making porter cheaper, since it could now be brewed from almost all pale malt, which gave a much better extract for the cost compared to the mixture of pale and brown malts that had been used before. While Irish brewers (or at least Guinness) went over eventually to using entirely pale malt and patent malt to make their stouts and porters, London brewers, at least, used a mixture of pale, brown and roast malts in their stouts and porters, though some continued to use just pale and brown malt.
At Whitbread, whose Chiswell Street premises, on the edge on the City of London proper (the "Square Mile"), was one of the top two or three London porter breweries, in 1805 the firm used 160 quarters of pale malt and 56 quarters of brown malt to make both its porter and single stout, and 136 quarters of pale together with 40 each of amber and brown malt to make its double stout. From these 216 quarters of malt it would make 798 barrels of porter (3.7 barrels to the quarter, around 6 per cent alcohol by volume), or 720 barrels of single stout (7 per cent abv or so) or 580 barrels of double stout (2.7 barrels to the quarter, around 8 per cent abv).
Four decades later, in 1844, the Whitbread brewing books show it was making five different black beers: porter, KP (keeping porter), single stout, double stout and triple stout, all from pale malt and brown malt only, in a ratio of approximately three quarters pale to one of brown. The stouts were generally parti-gyled with one or other of the porters, that is, the stout was made from a first, strong mashing, while the porter was made from a second mashing of the same grain, possibly mixed with a third or four mashing. The only difference between the stout and the porter, therefore, was in strength.
Ron Pattinson's research shows that in the second half of the 19th century, London brewers' recipes for porter and stout began to diverge – but the stouts were now getting LESS patent malt, where used, than the porters, and more brown malt, with the probable result that the stouts were becoming sweeter and less dry than the weaker porters.
Roasted barley became a permitted ingredient in beers in the United Kingdom (which still included Ireland at this time) in 1880, with the passing of the Free Mash Tun Act. However, there was no rush to start using it: one writer in 1885 insisted that roasted barley did not give as permanent a colour as roasted malt, and "the flavour is also very inferior; and the aroma can bear no comparison." Guinness in particular does not appear to start using roasted barley in its stouts until around 1930.
The First World War, and the high taxes and restrictions on beer that it brought, did terrible and permanent damage to the strengths of beers in the British Isles, and by the 1920s porters were down on average to around 3.6 per cent alcohol by volume, and stouts to only 4.7 per cent or so. Guinness Extra Stout, which had been around 7.7 per cent alcohol in the 19th century, was down to 4.4 per cent in the 1930s.
When the "small brewer revolution" started in Britain and the United States in the 1970s, there was more than 250 years of different styles and strengths of stout and porter to choose from, and different brewers picked different sorts to recreate. The result is that, in some cases, some brewers now brew "porters" that are stronger than their stouts. The need to categorise beers for brewing competitions in the U.S. has meant a plethora of micro-managed style descriptions, few of which, however, have any historic validity.
The answer to the question: "What's the difference between a stout and a porter" is that originally a stout was simply a strong version of porter: today the difference is whatever you want it to be.