In Avengers: Endgame, Stark and Nebula are adrift in space on the Benetar.
How could Captain Marvel know to rescue them? Or was it just blind luck?
She was told to go save them by the other Avengers.
In Captain Marvel, we see Carol give Fury a space-page allowing him to contact her.
During the credits for Infinity War, we see Fury activate the space-pager right before he disintegrates.
During the credits for Captain Marvel, we see Carol being summoned to Avengers HQ by the space-pager, and meeting the Avengers, shortly after Thanos snaps everyone away.
If we assume that all of these scenes are canon (and not simply promotional material for the next movie), then it follows that scene 3 takes place while Tony and Nebula are drifting in space. We can thus assume that Steve and the gang told Carol to go look for them.
Captain Marvell has spread herself pretty thinly. As she says
There's thousands of worlds, all going through the same issues as you
So she's where she needs to be, when she needs to be there.
How she knows where she's most needed is unknown, but she turns up to rescue
Tony Stark and bring him home
when they (and the universe at large) needs it. It's clear that when she's not on-screen, she's off being super-heroic someplace else that really needs her.