The secret to the trick is called comparative advantage. Dave is better at making pies than Chris when compared to his ability to shovel driveways!. Consider the following: start out each of the neighbors making nothing but pies.

For Chris to shovel a driveway, he has to give up 2 pies. So a shoveled driveway is worth 2 pies to him.

For Dave to shovel a driveway, he has to give up 4 pies. So a shoveled driveway is worth 4 pies to him.

So Dave would happily give 3 pies for his driveway to be shoveled, for him a shoveled driveway is worth 4. And Chris is happy to accept 3 pies, he could only make 2 in the time it takes to shovel. Dave is giving up fewer pies to have Chris shovel his drive, than it would take to do it himself. Chris is gaining more pies than he could have made himself in the time it took to shovel Dave's driveway.

The two extra pies appear when Dave sticks to his specialty: making pies, and Chris performs the task he's best at: shoveling the driveway. This is also part of what makes specialization of labor work.

For completeness, it should probably be noted that if the trade-offs were the same, there would be no benefit (but also no loss). If Dave was slightly better at driveways, for example only taking 4 hours, no matter if he or Chris shoveled the driveways (or even if they didn't trade at all) there would always be 8 pies in total.