Well, 40k lore comes from three places generally:
-Codices. (Ranging from the 80s Rogue Traitor and Realms of Chaos books all the way up through today with all the different army codices.)
-Novels. (Everything published by the Black Library.)
-Really old stuff published in White Dwarf Magazine. (Also no longer in print.)
Codices can be broken down into army-specific books, and general books. GWS doesn't really do universe-setting books anymore, that's all roled into the Core Rule Book and isn't terribly concerned with ancient, ancient history. What universe-setting they did publish was made up primarily of Rogue Trader and Realms of Chaos. That's where a lot of the most ancient 40k lore resides (and much of it has been retconned.) All that stuff is out of print now.
The novels can roughly be broken down into two groups: Horus Heresy series, and not Horus Heresy. The vast majority of published novels are about either the Imperium or Space Marines specifically. Other races get a lot less play in those novels, but most races have at least one or two novels about them. (Can't remember if "'ere we go!" is an actual novel or a series of Ork short stories or what.) The order of most material published tends to go: Space Marines, Imperial Guard, Inquisition, Eldar, everything else.
So where to start? I'd probably say starting with the Horus Heresy will start you farthest back in the lore you can go without spending an arm and a leg on discontinued material on Ebay or Amazon. I would not recommend buying new codices just for the lore unless you have a shite ton of money to burn, and each Codex tends to be very specifically focused on the army in question. (Like, you're not going to get ALL SPACE MARINE HISTORY by buying a Blood Angles Codex.)
Most of my 40k lore knowledge comes from reading literally everything I can get my hands on that's 40k, over the last 20 years.
In a pinch though, just browsing all the topics on Lexicanum is a great start. It's all sourced (where it can be) and has generally decent overviews. It's also straight-laced. If you go to 1d4chan looking for 40k info, you'll find it, but with a ton of snark and memes thrown in to the mix.