Finally getting to the ending of that old tale. The version I know, which I think is more folklore than a single book, goes like this. The blacksmith, having outwitted the devil with the 'no work on Sundays' clause, gets dragged into one last wager. The devil challenges him to make a horseshoe that will never wear out. The blacksmith, clever as ever, forges one from cold iron, saying it will last as long as it's never used. The devil, furious at being tricked again, tries to claim the smith's soul anyway, but the contract's loophole protects him.
In the final scene, the devil is often banished by the sign of the cross on the anvil or by the blacksmith throwing a horseshoe at him. Some tellings have the devil fleeing when the rooster crows at dawn, leaving the smith free. It's a classic 'wit over strength' ending that always leaves me smiling. The smith keeps his soul and his forge, and the devil is left grumbling about the cleverness of humans. I heard a variation where the devil is so impressed he leaves the smith a bag of gold, but I prefer the straightforward victory.