The foil is simply to contain heat at the end where the compound is being generated.
The reaction only proceeds at temperatures approaching 100'C.
On the OTHER end, heat is something that is known to cause the product compound to explode, so it's kept in an ice bath.
The foil is insulation.
Don't you love my tinfoil sculpture? It reminds me of that awesome movie 'real genius' wiht the giant tinfoil popcorn ball...