I've gotten to work with a lot of amazing silicon valley engineers who are
here on H1B visas. As as I understand, losing your job does not normally
mean being sent back to your country since you can transfer the H1B visa
to your new employer.
The tough part is just getting the H1B visa since you must have a company here sponsor you to get it. This is why the monthly "Who's Hiring" submission here on HN requests that posters declare whether H1B's are welcome.