I'm not sure if you can somehow find out the price of an item directly... if not, I would do it via some dummy unit specifically meant for this. This dummy unit would have the backpack ability.
You would need to keep check of all heroes that player has.
Then when player leaves, you remove all items from every hero, then one by one you give it to the dummy unit and make it sell that item.
I don't really know how else you could get the sum of gold from every item that player had.
Once that is done, then it is simply a matter of picking all units of said player and removing them; and taking his gold and splitting it between his allies (sum each player gets = gold of leaving player / number of his allies).
Note though, that the sum may not be exactly split.
Gold is an integer number (number without decimal point), which means everything behind decimal point is lost when that number is converted into integer.
E.g. 44/5 = 8.8 --> converted to integer (= all behind decimal point is lost) --> 8
When you calculate it back, 5*8 = 40, so you ultimately lost 4 gold.
Unless you make the gold system more complicated so it will ~evenly split the gold between allies, you will always have inaccurace ranging between 0 to (number of allies -1).