An exempt organization is not taxed on its income from an activity that is substantially related to the charitable, educational, or other purpose that is the basis for the organization's exemption. Such income is exempt even if the activity is a trade or business.
However, if an exempt organization regularly carries on a trade or business that is not substantially related to its exempt purpose, except that it provides funds to carry out that purpose, the organization is subject to tax on its income from that unrelated trade or business.
See updated IRS Publication 598 on UBIT.