Appendix A – Internal Revenue Code 7701(b)(3)
It all starts with Internal Revenue Code 7701(b)(3).
(3) Substantial presence test
(A) In general Except as otherwise provided in this paragraph, an individual meets the substantial presence test of this paragraph with respect to any calendar year (hereinafter in this subsection referred to as the “current year”) if—
(i) such individual was present in the United States on at least 31 days during the calendar year, and
(ii) the sum of the number of days on which such individual was present in the United States during the current year and the 2 preceding calendar years (when multiplied by the applicable multiplier determined under the following table) equals or exceeds 183 days:
(B) Exception where individual is present in the United States during less than one-half of current year and closer connection to foreign country is established An individual shall not be treated as meeting the substantial presence test of this paragraph with respect to any current year if—
(i) such individual is present in the United States on fewer than 183 days during the current year, and
(ii) it is established that for the current year such individual has a tax home (as defined in section 911(d)(3) without regard to the second sentence thereof) in a foreign country and has a closer connection to such foreign country than to the United States.
(C) Subparagraph (B) not to apply in certain cases Subparagraph (B) shall not apply to any individual with respect to any current year if at any time during such year—
(i) such individual had an application for adjustment of status pending, or
(ii) such individual took other steps to apply for status as a lawful permanent resident of the United States.
Appendix B – The Treasury Regulation governing the “closer connection” exemption
§ 301.7701(b)-2 Closer connection exception.
(a) In general. An alien individual who meets the substantial presence test may nevertheless be considered a nonresident alien for the current year if the following conditions are satisfied—
(1) The individual is present in the United States for fewer than 183 days in the current year;
(2) The individual maintains a tax home in a foreign country during the current year; and
(3) Except as provided in paragraph (e) of this section, the individual has a closer connection during the current year to a single foreign country in which he or she maintains a tax home than to the United States.
(b) Foreign country. For purposes of section 7701(b) and the regulations thereunder, the term “foreign country” when used in a geographical sense includes any territory under the sovereignty of the United Nations or a government other than that of the United States. It includes the territorial waters of the foreign country (determined in accordance with the laws of the United States), and the seabed and subsoil of those submarine areas which are adjacent to the territorial waters of the foreign country and over which the foreign country has exclusive rights, in accordance with international law, with respect to the exploration and exploitation of natural resources. It also includes the possessions and territories of the United States.
(c) Tax home—(1) Definition. For purposes of section 7701 (b) and the regulations under that section, the term “tax home” has the same meaning that it has for purposes of section 162(a)(2) (relating to travel expenses while away from home). Thus, an individual’s tax home is considered to be located at the individual’s regular or principal (if more than one regular) place of business. If the individual has no regular or principal place of business because of the nature of the business, or because the individual is not engaged in carrying on any trade or business within the meaning of section 162(a), then the individual’s tax home is the individual’s regular place of abode in a real and substantial sense.
(2) Duration and nature of tax home. The tax home maintained by the alien individual must be in existence for the entire current year. The tax home must be located in the same foreign country for which the individual is claiming to have the closer connection described in paragraph (d) of this section.
(d) Closer connection to a foreign country—(1) In general. For purposes of section 7701(b) and the regulations under that section, an alien individual will be considered to have a closer connection to a foreign country than the United States if the individual or the Commissioner establishes that the individual has maintained more significant contacts with the foreign country than with the United States. In determining whether an individual has maintained more significant contacts with a foreign country than the United States, the facts and circumstances to be considered include, but are not limited to, the following—
(i) The location of the individual’s permanent home;
(ii) The location of the individual’s family;
(iii) The location of personal belongings, such as automobiles, furniture, clothing and jewelry owned by the individual and his or her family;
(iv) The location of social, political, cultural or religious organizations with which the individual has a current relationship;
(v) The location where the individual conducts his or her routine personal banking activities;
(vi) The location where the individual conducts business activities (other than those that constitute the individual’s tax home);
(vii) The location of the jurisdiction in which the individual holds a driver’s license;
(viii) The location of the jurisdiction in which the individual votes;
(ix) The country of residence designated by the individual on forms and documents; and
(x) The types of official forms and documents filed by the individual, such as Form 1078 (Certificate of Alien Claiming Residence in the United States), Form W-8 (Certificate of Foreign Status) or Form W-9 (Payer’s Request for Taxpayer ldentification Number).
(2) Permanent home. For purposes of paragraph (d)(1)(i) of this section, it is immaterial whether a permanent home is a house, an apartment, or a furnished room. It is also immaterial whether the home is owned or rented by the alien individual. It is material, however, that the dwelling be available at all times, continuously, and not solely for stays of short duration.
(e) Special Rule. An alien individual may demonstrate in one year that he or she has a closer connection to two foreign countries (but no more than two) if he or she satisfies all of the following conditions—
(1) The individual maintains a tax home beginning on the first day of the current year in one foreign country;
(2) The individual changes his or her tax home during the current year to a second foreign country;
(3) The individual continues to maintain his or her tax home in the second foreign country for the remainder of the current year;
(4) The individual has a closer connection to each foreign country than to the United States for the period during which the individual maintains a tax home in that foreign country; and
(5) The individual is subject to taxation as a resident pursuant to the internal laws of either foreign country for the entire year or subject to taxation as a resident in both foreign countries for the period during which the individual maintains a tax home in each foreign country.
(f) Closer connection exception unavailable. An alien individual who has personally applied, or taken other affirmative steps, to change his or her status to that of a permanent resident during the current year or has an application pending for adjustment of status during the current year will not be eligible for the closer connection exception. Affirmative steps to change status to that of a permanent resident include, but are not limited to, the following—
(1) The filing of Immigration and Naturalization Form I-508 (Waiver of Immunities) by the alien;
(2) The filing of Immigration and Naturalization Form I-485 (Application for Status as Permanent Resident) by the alien;
(3) The filing of Immigration and Naturalization Form I-130 (Petition for Alien Relative) on behalf of the alien;
(4) The filing of Immigration and Naturalization Form I-140 (Petition for Prospective Immigrant Employee) on behalf of the alien;
(5) The filing of Department of Labor Form ETA-750 (Application for Alien Employment Certification) on behalf of the alien; or
(6) The filing of Department of State Form OF-230 (Application for Immigrant Visa and Alien Registration) by the alien.
(g) Filing requirements. See § 3O1.7701(b)-8 with regard to the statement that must be filed by an alien individual claiming the closer connection exception.
[T.D. 8411, 57 FR 15244, Apr. 27, 1992; 57 FR 28612, June 26, 1992; 57 FR 37190, Aug. 18, 1992; 58 FR 17516, Apr. 5, 1993]
https://www.law.cornell.edu/cfr/text/26/301.7701(b)-2
Appendix C – Closer Connection Exemption The Substantial Presence Test
Appendix D – IRS Form 8840
Notice that the questions are really aimed at determining where you are “ordinarily resident”. Notice how the questions in this form compare to the Canada Revenue Agency’s form NR73 which is used to determine whether somebody is “ordinarily resident” in Canada (and therefore a tax resident of Canada).
https://www.irs.gov/pub/irs-pdf/f8840.pdf
Appendix E – Canada Revenue Agency Form NR73
https://www.canada.ca/content/dam/cra-arc/formspubs/pbg/nr73/nr73-23e.pdf