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.
Certainly, it seems to be blind luck but it's possible that Captain Marvel was tracking Thanos and was heading to Earth after visiting Titan.
Since the Benetar was heading back the same way it's reasonable to assume that Captain Marvel would have come across the ship on the way.
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.
The ship must have sent some distress SOS signal for 21-22 days in a row. Plus the ship shouldn't be far from Titan. Obviously we don't how long Captain Marvel have spent searching for them in the open universe.