We have two main sources of these things. One is small asteroids that orbit the Sun for millions of years before entering the Earth's atmosphere, and the other is chunks breaking off comets, which move on average about 25 times as fast as those asteroids when passing through our neighborhood heading toward the Sun. Those could be more destructive, because of their higher velocities but at least travel with comets, the largest of which are hopefully better tracked. The house-sized Earth-crossing asteroids go mostly untracked, even though supposedly most mile-wide rocks are known and their trajectories are calculated. But some pretty big ones aren't spotted until after passing by, since they come at us from the direction of the Sun.
But even if we spot an extinction-level rock coming a few years ahead, what could we do about it if mighty Federation starships on Trek couldn't divert them?