How do states know I lived there?

They look at your W-2s, which your employer is required to report based on where the work was performed. If you move mid-year, you’ll likely receive a W-2 showing income for multiple states.