Memorized Spellcasters get their new spells immediately; Prepared Spellcasters must wait for a Long Rest
\begin{array}{|l|l|}
\hline
\text{Memorized} & \text{Prepared} \\ \hline
\text{Arcane Trickster (Rogue)} & \textit{Artificer (Revised, UA)} \\
\textit{Artificer (Original, UA)} & \text{Cleric} \\
\text{Bard} & \text{Druid} \\
\text{Eldritch Knight (Fighter)} & \text{Paladin} \\
\text{Ranger} & \text{Wizard}\\
\text{Sorcerer} & \\
\text{Warlock} & \\ \hline
\end{array}
Each of the Memorized spellcasters have the following passage in their Spellcasting feature paragraph, pending a few discrepancies depending on how many spells the class gets in total.
You know four 1st-level spells of your choice from the [class] spell list.
You learn an additional [class] spell of your choice at each level except at levels [...]. Each of these spells must be of a level for which you have spell slots. For instance, when you reach 3rd level in this class, you can learn one new spell of 1st or 2nd level.
Additionally, when you gain a level in this class, you can choose one of the [class] spells you know and replace it with another spell from the [class] spell list, which also must be of a level for which you have spell slots.
—The Common Spellcasting paragraph for Bards, Sorcerers, Warlocks, etc., Player's Handbook, pg. 45-120
So as written, the character would gain these spells immediately at the time they level up.
Prepared spellcasters, meanwhile, are forced to wait for a level up:
You prepare the list of [class] spells that are available for you to cast, choosing from the [class] spell list. When you do so, choose a number of [class] spells equal to your [Spellcasting Ability] modifier + your [class] level (minimum of one spell). The spells must be of a level for which you have spell slots.
For example, if you are a 3rd-level [class], you have four 1st-level and two 2nd-level spell slots. With a [Spellcasting Ability] of 16, your list of prepared spells can include six spells of 1st or 2nd level, in any combination. If you prepare the 1st-level spell cure wounds, you can cast it using a 1st-level or 2nd-level slot. Casting the spell doesn't remove it from your list of prepared spells.
You can change your list of prepared spells when you finish a long rest. Preparing a new list of [class] spells requires time spent [...]: at least 1 minute per spell level for each spell on your list.
—The Common Spellcasting paragraph for Clerics, Druids, Wizards, etc., Player's Handbook, pg. 45-120
So a prepared spellcaster would not gain their new spells until they take a long rest.
Well... Almost. There are exceptions for Prepared Spellcasters
Many prepared Spellcasters gain additional spells automatically as they level up. For example, a Land Druid has a list of spells they gain automatically at levels 3, 5, 7, and 9. These spells are gained immediately upon level up, no need to wait for a long rest.
Once you gain access to a circle spell, you always have it prepared, and it doesn't count against the number of spells you can prepare each day. If you gain access to a spell that doesn't appear on the druid spell list, the spell is nonetheless a druid spell for you.
—Circle of the Land, Player's Handbook, pg. 45-120
Additionally, while Wizards would not get to add any new spells to their Prepared Spells list, they would get to immediately add any new spells to their Spellbook. Because Wizards are ritual casters, and are not required to have their Ritual spells prepared to cast them as a ritual, a Wizard would be able to cast any new Ritual spells immediately, without needing a Long Rest.
You can cast a wizard spell as a ritual if that spell has the ritual tag and you have the spell in your spellbook. You don't need to have the spell prepared.
—Wizard, Player's Handbook, pg. 45-120
All spellcasters gain their new Spell Slots immediately, if applicable
The rules in the Player's Handbook for each spellcasting class indicates that a long rest is required to recover expended spell slots, but there's nothing written that says they do not immediately gain access to new spell slots they attain.
So if a Moon Druid (does not gain extra spells at 3,5,7,9) goes from level 4 to level 5, they would not have access to any new spells; they would, however, have two new 3rd level spell slots to use, even if all their other slots were already exhausted, without needing to take a long rest.
The [class] table shows how many spell slots you have to cast your druid spells of 1st level and higher. To cast one of these [class] spells, you must expend a slot of the spell's level or higher. You regain all expended spell slots when you finish a long rest.
—The Common Spellcasting paragraph for all spellcasters, Player's Handbook, pg. 45-120
Warlocks have some special rules, but are otherwise identical to Memorized spellcasters
At least within the context of this post. I just want to call them out specifically because they have the ability to recover spell slots on a Short Rest, instead of a Long Rest. So because of this, like other Memorized Spellcasters, they gain their new spell(s), if applicable, immediately.
However, their new spell slots is a different issue. Their spell slots "upgrade" to the higher levels instead of gaining new spell slots, so unless a Warlock actually gains a new spell slot (like at levels 2, 11, or 17) their upgraded spell slots would not be refreshed, and they'd still have to take a short rest to gain those slots back.