Considering that factories required railways to transport materials in and out of them, what came first? Were the factories first, because then there was an economic incentive to build a railway to that area or was it the railway that suddenly allowed the condition for a factory to be opened?
You're right, that a factory owner would find mass transport useful. A factory is taking advantage of economies of scale, is trying to do mass production in order to lower the price per item. That means, as you note, lots of material and supplies have to be transported in, and lots of finished goods have to be transported out.
Before the Industrial Revolution, whenever anyone needed to move a lot of stuff over the longest distance for the least money, if they could they used a boat. A boat is a quite amazing machine, really: at very slow speed it has almost no friction. A pair of mules can easily pull 50,000 pounds of canal boat along a canal for days, and so canals were considered quite early. The Midi Canal in France was started in 1666, dug during the reign of Louis XIV.
In some very important ways, the Industrial Revolution started in England in the middle of the 18th c.. It was before any practical steam-powered transportation. There was, therefore, in the middle of the 18th c., a lot of interest in England in canals. A good example of a factory owner involved in this would be Josiah Wedgewood, who had managed to mass-produce pottery in his shops at Stoke on Trent that imitated higher-priced imported porcelain. Hauling it in wagons over the usual rough roads resulted in a lot of breakage. Wedgewood and engineer James Brindley started digging the first of what would be a whole series of canals in 1766, which by the end of the century became a network of over 365 miles, connecting the Midlands with the Thames and Mersey rivers.
Steam transport would come into this picture rather late. Although steam-powered boats would be demonstrated before the end of the century, steamboats would not become common until after Fulton in 1809. A gravity-powered rail system was in use in coal mines somewhat earlier, but the first steam railroad began with Richard Trevithick, in 1804. By then, canals had been important in England for decades,
Canals would be a very important means of transport for bulk materials throughout the 19th c. Most of New York City's grain and coal would come in on the Erie Canal, then be brought down the Hudson River. However, canals could not go everywhere- hard to run one up a mountain, or through a desert- and so as the network of railroads increased canals became more and more marginalized. A few continue to be commercially viable. Some, like the C& O Canal became unprofitable and mostly abandoned ( it's now a National Park) and some, like the Erie Canal, are in use but have become more useful for recreation than mass transportation.
Samuel Smiles: The Lives of the Engineers
In most of the United Kingdom, the industry was followed by the railways which in turn helped the industry grow. A very good example of this is the village of Coalbrookdale in Shropshire, which is also a very good microcosm of England and a good gauge for the whole country (it's a fascinating area and one whose fortunes almost perfectly parallels the country's).
Fro context, Iron was the industry of Coalbrookdale and the area had been renowned both nationally and internationally for the high quality metal that was produced there. This very fact is reason that Abraham Darby moved from Bristol to Shropshire to develop his latest smeling methods and his grandson built the World's first Iron bridge there in 1779. The area was growing but not at a particularly fast rate nor particularly prosperous. Despite the first working, reliable steam locomotive being built there in 1802, the company wasn't worth a huge amount, but industry was there.
The railway first came to the gorge in 1849, and over the next 17 years 4 lines were built nearby, two with direct access to the village. This period also sees the value of the company, the size of the village both grow and the demography of the area experience it's greatest change of the 19th century.
But what also came with it was new industries. People from other large manufacturing areas, especially those with links to metal work and specifically Sheffield, came to live and work in Coalbrookdale, bringing with them new expertise. Smaller, at-home workshops were set-up for plasterers, glass-blowers, as well as more iron companies.
The railway followed the industry and enhanced it. Faster goods travel, more reliable personal transport, and greater speed of news, all brought a boost to small, regional industries.
Sources
Leaflet, ‘the Works of the Coalbrookdale Company: Mercantile Gazette’, 1878, Ironbridge Gorge Museum Library and Archive, 669.1(085)/CBD
G. Best, Mid-Victorian Britain: 1851- 75, (Fulham: 1985)
B. Trinder (ed.),* A Description of Coalbrookdale in 1801 A.D*.,(Wolverhampton: 1987)