George R.R. Martin is a co-executive producer for the show and in earlier seasons wrote at least one episode.
The producers David Benioff and Dan Weiss even addressed this particular issue back in 2014 prior to the start of Season 4:
“Last year  we went out to Santa Fe for a week to sit down with [Martin] and just talk through where things are going, because we don’t know if we are going to catch up and where exactly that would be," Benioff says in the April issue of Vanity Fair. "If you know the ending, then you can lay the groundwork for it. And so we want to know how everything ends. We want to be able to set things up. So we just sat down with him and literally went through every character.”
Martin noted that not all of the pieces of the puzzle are in place. “I can give them the broad strokes of what I intend to write, but the details aren’t there yet," he tells Vanity Fair. "I’m hopeful that I cannot let them catch up with me.”
While in recent years George has pulled back from the show to focus on The Winds of Winter (or at least that is the offical answer, hard-core fans believe that GRRM and D&D have had creative differences that have caused Martin to withdraw his guidance) he had already given the show creators enough information to complete series.
It the end, D&D have had to fill in some of the gaps with their own stories. Both the show and the books know their end, which is has been stated to be the same, but are taking different routes to get there.
[Q] You’re now at a point where you’ve caught up with the books. What does that mean for the future?
Benioff: Season five is still very much within the books for the most part. The very first scene of the season and the very last scene of the season are book scenes. It’s more season six that’s going to be diverging a bit. We’ve had a lot of conversations with George, and he makes a lot of stuff up as he’s writing it. Even while we talk to him about the ending, it doesn’t mean that that ending that he has currently conceived is going to be the ending when he eventually writes it.
Weiss: It’s like looking at a landscape and saying, “OK, there’s a mountain over there, and I know that I’m getting to that mountain.” There’s an event that’s going to happen, and I know that I’m moving in the general direction of that event, but what’s between where I’m standing now and that thing off on the horizon, I’m not totally sure. I’ll know when I get there, and then I’ll see what the terrain looks like around me and I’ll choose my path once I get closer to it. He figures a lot of this stuff as he goes. He always says he’s a gardener, not an architect.
[Q] You’ve had to make your own editing choices as you’ve made the show.
Benioff: One of the most common questions we’ll get asked is, “Why did you change this from the books?” The answer is always the same, really. It’s just because we thought it would be better for the series. Some of them are really fun characters but we already have the largest cast I think in television history and it just seems to grow every year.