I've read a lot around this site that an ideal quality of the lens is achieved when using f-stop roughly 2 stops slower than the fastest value of the lens.
Firstly, I'm not exactly sure what "2 stops" mean when it comes to f-stop, but this isn't really important in what I'm about to ask. What I'd like to know is what is the base f-stop when counting this value.
Let's look at it this way: If I have a prime lens with f/1.8, then obviously I'll be getting the ideal value from f/1.8, let's say after 2 stops I get f/2.8?
But! What in case of zoom lenses, for example 18-55, which have f-stop f/3.5 - f/5.6. Should I take f/5.6 as my base value, or does the base value depends on the real fastest value at the exact focal length?
Therefore, if I set the lens to 18 mm, their fastest aperture is f/3.5 and thus the ideal quality is at f/5.6 and when I set it to 55 mm, their fastest aperture is f/5.6 and thus the ideal quality is at f/8 ? Or is it f/8 for the whole focal range ?
Real fastest value at the exact focal length.
But, this is just a rule of thumb — it's not necessarily exactly two stops in every case.
For the question of what two stops mean, see What does f-stop mean?. In short, each stop is approximately the-square-root-of-two times the previous one. That means half the light is allowed in (which is why the seemingly-weird series of numbers was chosen). Two stops is doing that twice, which conveniently works out to simply being doubling. Two stops from f/2 is f/4, and and two stops from that is f/8. Or, starting at f/1.8, two stops is f/3.6.
The test results comparison tool used by Digital Photography Review is interesting for looking at this. Try this test of Pentax's 18-55mm zoom, which like most kit zooms goes from f/3.5 at the short end to f/5.6 zoomed out.
You can see that at 18mm, the sharpness improves as you go from f/3.5 to f/4 to f/5.6. At f/8, it's more even from center to corners, but doesn't get any sharper. And beyond that, it drops off.
Then, change the focal length to 55mm. You'll notice that with this lens, overall sharpness goes down from 18mm at f/5.6. At f/8, it picks up a little bit, and a little more at f/11, and then back down again at f/16.
So, that fits with the two-stops guideline pretty well. Same for the Canon kit lens, and Nikon too.
With the Pentax DA 15mm f/4 Limited test, they've got test results in third-stops, and there you can see that f/7.1 is no better than f/6.3, and f/8 is worse — so that's only 1⅓ stops. On the other hand the DA★ 55mm f/1.4 shows peak sharpness at f/4.5 — closed down by 3⅓ stops. This doesn't reflect a problem with the lens, just different priorities in design. The Nikon 50mm f/1.4G is similar, with peak sharpness at 3⅔ or 4 stops down. However, the entry level Nikkor f/1.8G fits the "two stops" guide quite well, peaking at around f/3.5.
It's also worth noting that this test is primarily concerned with sharpness, because that's easy to measure. Other image quality and rendering characteristics are also affected. Vignetting (light falloff in the corners) gets better the more you stop down, and it's usually gone after two stops. And bokeh quality is usually improved by stopping down, too — generally, lens bokeh is nicer/smoother stopped down a bit, but of course it's less visible since you have greater depth of field. Plus, shape of the aperture blades will become visible in specular highlights, which is a side-effect unrelated to stopping down per se — some modern lenses have rounded aperture blades to make that not be a concern.