I also struggle with this! It depends on a lot of factors and your environment.
For example, as a PhD student I share an office with several other PhD students. Sometimes when we're finding it hard to concentrate, me and my friends who are there will decide we're 'locking in' for a set amount of time (usually one hour). This is an opportunity to do our own thing - usually each with headphones on - but holding each other accountable. You can't stop working because then you'll lose. Can't be the one to break the lock-in before time is up.
Similarly, as an undergrad I'd work near my friends who were also working. You use each other accountability buddies. You;re in charge of making sure they don't distract themselves and vice versa. It's very fun to relentlessly mock one another for checking phones etc.
But sometimes you don't have access to other people. Today, I'm working from home. It's nearly 2pm and I'm distracting myself from work! But that's okay because my method of work is check point based. You write down the one big task to do today, and any little tasks. When I work from home, often the little tasks are "put laundry in the machine" or "answer the email". You need to start your day off with a couple of little tasks - ideally at least one is work related, though I use personal ones too. Then you can start on the big task. Set a time limit - maybe 30 mins - to just work and then go do a little task. Adding in 20 minute breaks are encouraged!! But plan where you're going to put them first.
Now, for my ultimate hack. For my little tasks, sometimes they include "open the book to the right chapter" or "open the document". That sounds super silly but honestly getting started is the hard part. But once you're started it's usually easier!
Look, you got this. I believe you can do that study, that assignment, that coursework. Whatever it is, take a breath and know that you can do it, you just gotta start.