It’s flat out against the law to replace an American worker with an H1B worker.
http://www.dol.gov/whd/regs/compliance/FactSheet62/whdfs62N.htm
How do they get away with this ?
By outsourcing the functions to a separate company. That way they aren't firing an American worker and replacing him with an H-1B. They are firing the worker because the position isn't there anymore. It's with the outsourcing company, who can hire whoever they want.