McCain didn't fly back to Washington just to vote "no" on the "skinny repeal" bill. That was just the final result of the entire process which no one saw coming.
Tuesday
First of all, he flew back to cast a vote on the "motion to proceed" to debate about passing a health care bill. From FiveThirtyEight's live blog's opening post on Tuesday morning:
Today’s vote will have extra drama because Arizona Sen. John McCain, recovering from a surgery and recently diagnosed with brain cancer, has opted to return to vote on this motion. It’s hard to imagine that he rushed back to vote against his party leadership and a Republican president. On the other hand, it’s hard to imagine he rushed back unless this was likely to be a very close vote. Collins, Paul and Nevada’s Dean Heller, in particular, have harshly bashed the various repeal and replace ideas McConnell has put forth. If this motion passes, as expected, one of that trio will have to vote for it. (There are 52 Republicans in the Senate and at least 50 must back this for it to pass.) And they will have to do a lot of explaining for that move. (It looks like Paul will back the motion to proceed.)
He then voted "yes" on the motion to proceed, and gave a speech which called for a return to "regular order" in the Senate. (This process was not in any way "regular").
This chart shows that after voting to proceed, he then voted for the "Repeal and Replace" variant of the bill (failed by 7 votes) and against the "Repeal and Delay" variant (failed by 5), even though he'd voted for the latter in 2015. That takes us through Wednesday, and the end of all previously released bills.
Thursday day
On Thursday, there was a lot of talk about a "skinny repeal", but no one saw the text until Thursday night. In the meantime, McCain and several other Senators called for passing a bill just to go to conference with the House, but only with assurances that the "skinny repeal" would not ever become law. In reply, Speaker Paul Ryan said:
that “if moving forward requires a conference committee, that is something the House is willing to do.” That’s a slightly more tepid and conditional assurance than [Nate Silver would] have expected, but we’ll see how Graham and other holdouts react — they may not need all that much to vote “yes.”
It's unclear why the "skinny repeal" wasn't instead replaced with a clearly placeholder bill if Senators were being asked to vote on it "just to get it to committee". McCain considered Ryan's assurances "not sufficient", but McConnell brought the bill to the floor for a vote anyway.
Thursday night
To once again quote from the live blog:
The one thing I’d say is that sometimes undecided votes really are undecided. The last public signal McCain had given, a few hours ago, was that he wasn’t assured by Paul Ryan’s statement. Everyone (including us) had assumed that those undecideds would probably cave, because that’s what usually happens at the end of a close vote, and it’s specifically happened with the GOP moderates (including McCain) several times in this process. Still, McConnell and Co. might have counted their chickens before they hatched.
and
I think most Senate reporters thought this was going to pass once Lindsey Graham said he was for it. That was reinforced when Pence headed over. And it was further reinforced when they opened up the roll call. However, McCain never said he was going to vote for it. And McCain plus Collins and Murkowski would be enough Republicans to stop it. I think most just assumed McCain would vote for it. Perhaps it was because Graham planned to or because McCain has a fairly conservative record. Whatever the case, it may have been a miscalibration.
Finally, at 1:30 in the morning, McCain voted "No". As the third "No" vote, that was enough to kill the bill (although the last two Senators then voted "yes" after him). If you read the rest of the blog from there, you can read some very surprised political commentators trying to come to grips with it.
What if he wasn't there at all?
If McCain hadn't come back at all, then there would have only been 99 Senators on the floor. In that circumstance, 50 is still enough to pass any legislation, but Pence's tie-breaking vote wouldn't have been needed. So if he had remained in the hospital or at home, he wouldn't have had any say or changed the numbers that the Republicans needed. But with two GOP Senators committed to voting against this, there would only be 49 possible "yes" votes without him.