B-1 or B-2 visas: During your visit to the U.S., you may visit Canada or Mexico for up to 30 days and re-enter the U.S. as long as you re-enter within the period noted on the Form I - 94 which you received when you first entered.
For instance, if you come to the U.S. on July 10 on a B2 Visitor Visa, you may go to Canada and/or Mexico on or after November 10, and reenter the U.S. any time up until January 10. But because the six month period is up on January 10, you will also have to depart from the U.S. on that same day to avoid being an "overstay" (unless you applied for an extension of stay).
If you visit other countries such as England or Costa Rica, then return to the U.S., your re-entry will be considered to be a new admission, rather than a re-entry from a contiguous country in the course of your initial visit, and the admission inspection may be more strenuous. The CBP Officer inspecting you will want evidence that you intend to go back home to your country of citizenship to live as opposed to returning again and again to the U.S. after visits to other countries. Remember, a B1 or B2 visa allows you to come to the U.S. to visit. If the CBP Officer suspects that you are actually trying to be a de facto resident, you will be denied entry.
Re-entry is, of course, dependent on your continued eligibility to enter. If you have been arrested or committed an illegal act resulting in a warrant in your name since the time the visa was issued, you could be denied re-entry.