How'd they prevent such large metal machines from rusting?
It seems impossible to me, unless the whole thing was made from stainless steel. Even today, plenty of steel tools can still rust to ruination...
They painted them.
Just the same as cars get painted today to prevent rust, military vehicles were also painted, mostly for camouflage purposes but it had the side effect of preventing rust. Naval vessels in particular have to be painted to prevent corrosion.
What wasn't painted was greased or lubed with petroleum products.
Additionally, wastage in military vehicles in World war two was expected to be fairly high. Tanks and warplanes in particular were expected to have short service lives, perhaps a couple of years, and corrosion resistance was not typically a big concern.