No, you don't have to pay for spells you gain when leveling up
From the PHB, p. 114 sidebar:
Your Spellbook
The spells that you add to your spellbook as you gain levels reflect the arcane research you conduct on your own, as well as
intellectual breakthroughs you have had about the nature of the
multiverse. You might find other spells during your adventures. You
could discover a spell recorded on a scroll in an evil wizard's chest,
for example, or in a dusty tome in an ancient library.
Copying a Spell into the Book
When you find a wizard spell of 1st level or higher, you can add it to your spellbook if it is of a level for which you have spell slots
and if you can spare the time to decipher and copy it.
As you see, the rules establish a clear difference between spells that add to your spellbook and those that you find and copy into it. If you take a look at the next part, you will know why.
Copying a spell into your spellbook involves reproducing the basic
form of the spell, then deciphering the unique system of notation used
by the wizard who wrote it. You must practice the spell until you
understand the sounds or gestures required, then transcribe it into
your spellbook using your own notation.
For each level of the spell, the process takes 2 hours and costs 50
gp. The cost represents material components you expend as you
experiment with the spell to master it, as well as the fine inks you
need to record it. Once you have spent this time and money, you can
prepare the spell just like your other spells.
As you see, the process of copying involves a lot of time, effort and materials which are dependent on you not yet knowing the spell and reproducing it yourself. For spells which you gain as you level, you skip all of this. The final sentence in the section makes it clear that once the spell is copied through this process, you can prepare it just like your other spells. This implies you do not go through this process for your other spells and that this is a different method of adding a spell to your spellbook.
Lastly, on the same page you are given these simple instructions on what happens when you gain new spells and when you do so.
Learning Spells of 1st Level and Higher
Each time you gain a wizard level, you can add two wizard spells of your choice to your spellbook.
And there is nothing more to it (aside from level restrictions on the spells).
The way I've always thought of levelling up is this: It's an an abstraction of what would really be an ongoing process. A character doesn't instantly get better at a bunch of things once they've killed that 37th orc, they've actually been improving continuously the whole time, so leveling up us really just a 'tipping point' where the increase in skill becomes consistently effective.
In the case of a fighter, it would be them properly internalizing in muscle memory a technique they've noticed makes it a little easier to score a hit in some situations. They've known about it for awhile, but they don't always pull it off right (represented by random dice rolls). When they level up and get that extra BAB, they've got it down, and can do it every time.
For a wizard's spells, I imagine a wizard is constantly experimenting and doing research 'in the background'. When they level up, and get a new spell they've actually been working on that spell for a long time. They've only just has that final 'ah-ha!' moment to make the spell effective and reliable - good enough to use consistently. They still can't use it right away, because they need to finalize their notes and prepare it.
I also figure that levelling up doesn't neccessarily mean they can do the new thing 100% perfectly. +1 BAB (usually) doesn't mean you hit some enemies 100% of the time, so dice rolls still represent trying a new thing and failing. Things like feats and class features fit into this less well, but I like to think this explains things well.
Best Answer
The ranger can no longer cast replaced spells.
Rangers don't prepare specific spells from a larger set of known spells in the same way that some other classes do. Instead, like bards, they can always cast any of the spells they know, provided they have spell slots available.
This means that when a ranger chooses to replace one of the spells they know when they gain a level, they no longer know the old spell, and can't cast it at all. They can't change the spells they know again until they gain another level.