The justices affirmed a lower court's decision that had upheld the measure after it was challenged by TikTok, ByteDance and some of the app's users.