The basic algorithm is the dead chips (folds) + the minimum committed chips are distributed among the winners of the hand (main pot). If after this, there are still chips left in the pot, the process is repeated. If there are no winners in subsequent iterations, the committed chips are distributed back to their respective owners (for every side pot).
So let's say at the end of a hand there were three players all-in and $300 in dead chips:
P1 shoved $50, P2 shoved $100 and P3 shoved $150.
Scenario 1
If the hands were equal (3 way pot), everyone would get $150 in the first iteration.
In the next, P2 & P3 would collect an additional $50. In the final iteration, P3 would collect an additional $50.
So the winnings would be:
P1 $150
P2 $200
P3 $250
Scenario 2
Let us assume here that P1 wins.
P1 collects $450 (his $50 and $50 each from P2 & P3 + $300 dead chips).
In the next iteration, there are no winners solet's say neither hand wins outright (board flush). P2 gets $50 backwould collect side pot 1 ($50) and P3 gets $100 backwould collect side pot 2 ($100).