Few things are more frustrating to a DM than players who just aren't invested in the world or the story you're building for them. You've spent hours crafting props to give to them, which just get put in the corner and forgotten about. You've thought of fun NPCs you think they'll want to chat to, who are completely ignored and not interacted with at all. You're excited about the big plot twist reveal this session - but when it happens, all of your players have forgotten the various plot threads or characters you've woven into the story, and therefore nobody cares at all. I get it - this sucks. You invest a lot of yourself into being a DM, and sometimes you just don't feel like you get back what you put in. In truth, this is a bigger question than how to get your players invested in the story. This might instead be a question of am I playing with the right people? Or is DnD the right passtime for my group of friends? These are big questions, that you may need to do a bit of thinking over. If you're confident that those aren't the issues though, and that instead your players sometimes just need a bit of a nudge to become more invested in the world and story that you're building, then there absolutely are things that you can do to try and encourage that.
Importance of Session 0
A session 0 is super important for setting the tone of your campaign. It lets your players know the kind of world that your adventure is going to run in, and gives you the chance to tell them the style of DnD that you enjoy running. It also gives you a chance to help them build characters that fit into the world - so that they have a frame of reference about some of the organisations, major NPCs or political intrigues that are going on in the world. Of course, some of this you want to reveal as the campaign unfolds - but it's reasonable to think that these characters aren't coming into your story blind. They would have an awareness or understanding of much of what's going on in the world, even at the outset. Giving them this information helps to get them invested in this world, and also to build a character that makes sense within it. Furthermore, it also gives them a chance to have some degree of shared backstory with other players' characters. When I DM, I try to split my players into two or three groups, and ask their characters to have some degree of shared history together (can be either big or small). I've found that this gives characters more reason to work together, and also facilitates in-character roleplay and conversations - that really helps get players invested in the world.
Have story threads to pull for all players
The more detailed and interesting the players' backstories, the easier it then is for you as a DM to engage them in the plot. If they mention a dead family member, perhaps you could include a storyline where they find out the reasons for that family members death, or discover the perpetrator, or perhaps that the family member isn't dead at all. Don't worry if any of this sounds clichéed. It is, but your players won't really care. So long as you've got different story threads directly related to each player's character (or perhaps story arcs that relate to several players), that's a good way to try and get them engaged in your world.
To facilitate this, sometimes it's necessary to ask additional questions to your players out of game. What is your mother's name? How long ago did you leave your hometown to become a traveling bard? What is your relationship like with your brother? Whatever information is going to help you as a DM both flesh out the world and encourage your players to engage in it is fine. Now, try to be subtle about some of the information you ask. Did you actually see your father's dead body following the Orc attack? is a good way to alert your players to the fact that you want to write the supposedly dead father into the story, and therby ruining the surprise. So try not to be too obvious, and don't be afraid to ask a few red herring questions as well - but in general, don't worry about asking your players to elaborate or clarify parts of their backstory when necessary.
Make the sessions relevant
There's nothing that will draw your players out of your world quicker than if they feel like they're just going round in circles and not actually accomplishing anything in your sessions. Railroading gets a pretty bad rep in DnD circles - but actually a certain degree of it is sometimes necessary to give your players a little nudge in the right direction. Now, you could argue that in some settings there's no such thing as the 'right' direction, and that simply exploring the world is interesting enough in its own right - but I tend to find that players will engage more when that exploration helps to reveal pertinent plot points and drives the story along.
There will always be times when things go off the rails and players act in ways that you don't foresee - but having a frame of reference (a mystery for them to unravel, a particular NPC they need to meet, an event they need to stop happenning) will make it easier for you to keep their experience of the world as being interesting and relevant to them. This, in turn, should keep them more engaged in your sessions. And when they realise that by engaging in the world, they are likely to discover clues or items or people relevant to the story, they will be motivated to do so more and more. Again, a lot of this work is done in prep - either by having specific session plans that you want to go through, or by simply understanding the motivations of various NPCs or organisations, so that you can have them interact naturally with your players when they run into them. It takes a little work - but with time this will become increasingly natural, and your sessions should really benefit as a result.