The green card test is broader than most people expect. Section 7701(b)(1)(A)(i) treats an alien as a US resident if they are a lawful permanent resident "at any time during such calendar year." One day of the year is enough, and physical presence is not required, so a green card holder who spends the entire year in another country is still a US tax resident reporting worldwide income. Section 7701(b)(6) defines the status as having been "lawfully accorded the privilege of residing permanently in the United States as an immigrant in accordance with the immigration laws", where that status "has not been revoked (and has not been administratively or judicially determined to have been abandoned)." That last clause is why simply moving away does not end the tax obligation: the card has to be formally given up or determined to be abandoned.
There is one further exit written into the same paragraph, and it is easy to trigger without meaning to. An individual ceases to be treated as a lawful permanent resident if they begin to be treated as a resident of a foreign country under a tax treaty, do not waive the treaty benefits available to residents of that country, and notify the Secretary. Claiming treaty residence elsewhere therefore ends US tax residency, and can start the clock on consequences that belong to giving up the status.
The substantial presence test catches people who never planned on it. Section 7701(b)(3) requires at least 31 days of presence in the current year and a weighted three-year total of at least 183 days, counting all days in the current year, one third of the prior year's, and one sixth of the year before that. Because those weights add to one and a half, someone who spends about 130 days a year in the United States for work crosses it in the third year without anything changing in their life. The test has exceptions of its own, including a closer-connection escape and exempt-individual categories for students, teachers, trainees and certain visa-holders, which is where the detail lives.
The first-year election is narrow and deliberately awkward. Section 7701(b)(4) lets someone who does not meet the substantial presence test this year, was not a resident last year, but will meet the test next year, elect to be treated as a resident for part of this year. It requires at least 31 consecutive days of presence in the election year and presence on at least 75 percent of the days from the start of that 31-day run to the end of the year, with up to five absent days ignored. The election cannot even be made until the individual has met the substantial presence test for the following year, so it usually arrives with an extension or an amended return.
The first and last years are split years, not whole ones. Section 7701(b)(2) fixes the boundaries. A person who becomes a resident this year but was not one last year is treated as a resident only from the residency starting date: for a green card holder who does not also meet the substantial presence test, the first day of the year they were present in the United States while a permanent resident; for someone who meets the substantial presence test, the first day of presence in the year. Residency ends after the last day of presence, provided the person has a closer connection to a foreign country for the rest of the year and is not a US resident again the next year. A small amount of nominal presence at either edge can be disregarded, but section 7701(b)(2)(C)(ii) caps that at 10 days. The result is a dual-status year, in which the resident part is taxed on worldwide income and the nonresident part only on US-source income, on one return combining both.
Two elections can make a mixed-status couple file jointly. Section 6013(g) lets a US citizen or resident and a nonresident alien spouse elect to treat the nonresident spouse as a US resident, for all of the taxable year and for all subsequent years until the election terminates. Section 6013(h) handles the narrower case where one spouse was a nonresident alien at the start of the year and a resident at the end. Both open the door to a joint return, which is otherwise barred outright where either spouse was a nonresident alien during the year. Both also bring that spouse's worldwide income into the US tax base for the whole year, which is the trade being made. A section 6013(g) election that is once terminated cannot be made again by the same two people.
Resident status carries the reporting duties, not just the tax. The foreign-account and foreign-asset regimes attach to US persons, and a resident alien is one. The FBAR regulation at 31 CFR 1010.350(b)(2) defines a US person to include "an individual who is a resident alien under 26 U.S.C. 7701(b)", and the Form 8938 rules apply to specified individuals, which include resident aliens. Someone who becomes a US tax resident partway through a career abroad usually acquires a stack of reporting obligations on assets they have held for years, and the penalties for missing them are unrelated to how much tax is owed.