The best-tested treatment for difficulty falling or staying asleep is cognitive-behavioral therapy for insomnia, or CBT-I. These are the main principles:
1. Match the time you're in bed to the amount of time you can actually sleep.
When we're not sleeping well we tend to stay in bed longer to try to capture more precious sleep time. This strategy backfires as we usually don't get more sleep and just end up spending more time awake in bed, and feeling upset about not sleeping. Not a recipe for good sleep! So keep track carefully of your sleep for a week or so and figure out how many hours you're sleeping per night, on average.
For example, if a person is going to bed at 10 pm and getting up at 7 am, that's 9 hours in bed. Let's say it takes them an hour on average to fall asleep, and they're awake in the middle of the night for an hour. That means they're spending 9 hours in bed but only getting 7 hours of sleep.
In CBT-I, this person would start out going to bed at 12 am (midnight) and getting up at 7 am, 7 days a week.
If their sleep improves and they're able to sleep 85-90% of the hours they're in bed (around 6+ hours), then they would start going to bed at 11:45 pm and getting up at 7:00am. Every week or two they would add about 15 minutes in bed per night, until they were getting enough sleep to feel refreshed.
2. Get out of bed if you're not able to fall asleep.
This principle applies whether you're not able to sleep at the beginning, middle, or end of the night. I know this is not an easy guideline to follow—believe me. But we want to avoid spending time awake in bed because the mind will start to associate "bed" and "awake"—the opposite of what we want for good sleep. We definitely don't want to practice being worried and frustrated about our sleep while lying in bed. If we do that enough, we're going to start to associate "bed" with "anxiety."
So if you can't fall asleep easily, get out of bed and do something for a half-hour or so. Then get back in bed and see if sleep comes quickly. Repeat as necessary. Do the same thing if you wake up in the middle of the night. With practice, the mind will form a strong link between "Bed" and "Sleep."
3. Question the catastrophic thoughts we have in the middle of the night.
Most of us worry that we'll be "a wreck" the next day when we're not sleeping well. Most of the time, though, we do OK. We may not be at our best and we may be more irritable than usual, but it's not a disaster. We may also think we'll "never sleep," which obviously isn't the case. By addressing the worst-case scenarios our sleepless minds throw at us, we can quiet the mental activity that prolongs insomnia.
There are more components of CBT-I, including relaxation training, mindfulness, and sleep hygiene, but the three above in my view form the essential core of the treatment.