Some manner of virtual machine usually does the trick, as has been mentioned, but some old games are funny about that sort of thing and will still kick up a fuss.
And you can't install Windows 95 or 98 on a modern computer without virtualisation, as neither operating system will function properly with the hardware. Windows 98, for example, will not function properly if you install it directly on a computer (i.e. without using a virtual environment) with more than 512mb RAM.
So, as a few people have suggested, the ultimate solution is to keep a retro PC around. As somebody who has always been more fond of PC gaming than console gaming until the last few years, I have made a point to keep a PC for each sufficiently unique operating system, and that PC is always the highest possible spec that OS can support. So I have a cupboard with PCs for DOS 6 and Windows 3.1, Windows 98 and Windows XP (that is the PC I'm using to type this message actually).
For Windows 98, off the top of my head, you'd ideally need a Socket A AMD Athlon Thunderbird processor (1GHz will probably be enough), 512mb of SDRAM (was DDR RAM supported by 98, anybody? I can't remember the spec I've got on my 98 machine, but I think it's only SDRAM), a GeForce 3 Ti500 graphics card (I think the 4 series supported Windows 98 as well, but I'm not sure as that series came a year after Windows XP*) and an appropriate motherboard.
Don't forget that you'll also need a compatible power supply - I think 300w** would be enough. You may also need to secure a 3.5" floppy disk drive, particularly if the older games you're playing came on such a medium, but also because Windows 98 used floppy disks for some essential functions (remember boot disks?).
And even though people don't think about it very much, hard disk drive technology has changed considerably too - if I remember, Windows 98 had a maximum supported HDD size of either 64GB or 120GB*** depending on whether you had an updated version or not (early versions had a bug in FDisk I think). Also bear in mind that the BIOS chip on old motherboards may have a smaller HDD limit than even this, as low as 30GB on some older models from that era (but if you get a motherboard capable of supporting a later-edition Athlon Thunderbird I doubt this restriction will exist).
eBay often has old components for sale, but I've never tried buying any and can't vouch for how reliable that method would be.
Building an older computer isn't an easy undertaking, as finding compatible parts and matching them is even harder than it is when you're building a modern operating system, because they're not readily available. People have suggested buying an old PC from a car boot sale or something, but without knowing what spec the machine is some games may run poorly on it or not at all - it was very common for computers back then (and still, actually), particularly prebuilt machines, to lack 3D graphics cards. Some games could run in 'software' mode, but depending on the rest of the hardware in the computer, they may struggle badly. And the kind of people who are likely to be selling a PC that old are the kind of people who probably wouldn't have stuck an aftermarket graphics card in it.
These aren't consoles. PCs required frequent upgrades back then just as they do now - more frequent, perhaps, as things have slowed down a little. Processor power literally doubled every year for a while in that era, and just as now, games took advantage of these leaps in power. Which is why buying a random old PC might not suffice - you may end up with a PC barely capable of running the OS, let alone the games you want to play. If you're able to find the parts I listed earlier, however, you shouldn't have any problems with any games right up until the end of the Windows 98 era - assuming I remembered right, that should give you roughly the highest PC specification available to consumers prior to the emergence of Windows XP.
Windows 95 is a different story, though. I skipped that OS. From what I know, though, there's little to distinguish 95 from 98 under the hood, and from what I know moving from 95 up to 98 caused fewer compatibility issues than any other version of Windows before or since. Ideally, if you have a Windows 98 machine, you should be OK with Windows 95 software.
You should try virtualisation first. It'll probably work well enough, and it'll be a lot easier and cheaper. Unless you get lucky and find a high-spec Windows 98 machine for sale.
* And because the GeForce 4 series launched after Windows XP had been released, the only games that would need it would work on Windows XP anyway, negating the need for a retro machine. So stick with a 3 series.
** I'm guessing on this one, really, based on a vague memory. I seem to remember my Windows 98 machine having a 300w PSU, and it is a similar spec to the components I've listed, so it should be fine. If you actually decide to undertake building a retro PC, you should double check, but as I doubt you'll end up going to all this trouble I wouldn't worry about it!
*** Don't quote me on this, but I think it was Windows 98 First Edition that had the lower HDD restriction, and the issue was solved with Windows 98 Second Edition.