granitestone,
generally, out-of-status condition appears when one overstays his/her I-94 or violates conditions of current status. Now, overstaying I-94 is permitted under these circumstances (permitted = overstay time will not be counter towards unlawful presence):
a) EOS (extension of stay) application was timely filed (timely = before I-94 has expired);
b) Visa renewal (if applicable) application with EOS (extension of status) application were timely filed;
c) COS (change of status) application was timely filed;
d) AOS (adjustment of status) application is pending.
There is a little controversy about overstaying an I-94 obtained through use of Advance Parole document. But, at this time it is permissible to assume that a such overstay will not be counted towards unlawful presence as well.
Violation of conditions of current status is pretty simple. Each status is associated with terms and conditions. For example, B-1 status implies that you can stay in the States as long as your I-94 is valid and you are not filing for immigration and not engaging in unauthorized employment.
As for your particular situation, I can give you a few pointers. They are just pointers, not a legal advice. If your lawyer says that you should not travel because you may be subjected to 3- or 10-year re-entry bar, you should believe him/her, because s/he knows details of your case.
a) Travel is unadvisable for people who accumulated 180 or more days of unlawful presence. If the said time is between 180 and 365 days, then the 3-year re-entry bar applies. If the said time is more than 365 days, then the 10-year bar applies. However,
any unlawful presence is not good. Besides, you should realize that only one document actually can guarantee admission to the States and it is a USC Passport.
b) In regard of your "break of few weeks" between H-1B jobs. Well, technically, you were out of status as soon as your first H-1B job was terminated. Whenever an H-1B employee is terminated, s/he is required to leave the country immediately, or begin accrue unlawful presence. Your new H-1B, however, should have come without I-94 and you should have left the country and re-entered using the new visa.
On the other hand, you should review your H-1B out-of-status situation in historical perspective. In pre-2000 days, INS had different views on when a terminated H-1B employee begun accruing unlawful presence. I believe, they had 30-day "cushion" period at the time.
***
How do they know?
Hmm... not sure, but I suspect that they use a computer connected to a database.
Seriously, every time you surrender your I-94 when leaving the country, it is being forwarded to INS and they enter it to their database along with the date it was surrendered. That is how they can detect overstay. That is why it is very important to surrender all I-94's one has accumulated while being here.
As for H-1B employee termination, every employer is required by law to inform INS when it has happened, identifying the effective date of H-1B visa (and status) termination. Again, they enter this information to their database.
So, as it turns out, the procedure is pretty simple. And they
will know.