How can filling a tube with foam help prevent buckling?
A simplified analysis of column buckling shows that the axial load required for buckling is directly proportional to the second moment of area of the column's cross section: $F=\pi^2EI/(KL)^2`$`, where F is the buckling load, E is the elastic modulus of the material, I is the second moment of area, and KL is an effective column length based on the geometric length of the column (L) and a variable factor K that is based on the boundary conditions.  The critical term for this question, though, is I.

For example, look at the picture below of a beam that someone is trying to bend.  You can do this yourself with a ruler or a paperback book.



In the upper image, you're trying to bend the book/ruler by pressing down on the spine, and it's really hard to do.  Rotate the book 90 degrees and press down on the cover...and it's relatively easy (unless you picked a phone book).  The material of the book didn't change, only how the area of the cross section that you were bending was distributed in the direction that you were trying to bend the book.  In the same way, adding foam to the tube increases the total area that needs to bend in order for the tube to buckle, which, if you'll allow an approximation, increases the strength of the tube by 5% from the baseline case (a hollow, aluminum tube).  Double the radius, and that becomes 10%.  Double it again, and that goes to around 25%.  Don't take those numbers as gospel, but it should give you an idea that adding area -- even with a relatively low stiffness material -- you can increase the strength of a material significantly.

However, buckling is different from strength.  Buckling is a bending instability caused by compressive loads, and failures in buckling often occur well below the failure load for the structure.  Let's run some numbers for this, using the expression I mentioned back up at the top.

For example, if I run an analysis for a 4.5 ft long, hollow, 1 in diameter tube with a wall thickness of 1/16 of an inch made out of 7075-T6 aluminum (E = 10.3 msi) with both ends effectively pinned, I get a buckling load of 714 lbf (note: the axial compressive load that would make this structure yield would be closer to 8,000 lbf).  Naturally, one could fill that in with metal and increase buckling load dramatically to 1,727 lbf, and, while that's not the query, I am adding some bending stiffness to the tube by filling in the center.  Buckling of a composite column (insofar as I know) is tricky, but, here is an approximation (which will undershoot the actual buckling strength).

If I add foam and say that the foam will not buckle (as it is fully constrained by the tube and cannot deform in any direction save axially, let us say), and then assume also that the purely axial load on the column will be divided between the foam and the aluminum according to the proportion of their elastic moduli (polystyrene has a Young's modulus of about 2.5 GPa), then the aluminum will only absorb about 88% of the load placed on the column.  This increases the buckling load by about 100 lbf (without causing the foam to yield).  However, this does not take into account how the foam will add bending stiffness to the structure, which would further increase the benefits of adding the foam.

In short, you're using a low-weight, cohesive filler material to increase the bending stiffness of a structure, dramatically increasing its tolerance for compressive loads.