From your limited description, it could be a few different things.
1. If it, at one point, stayed in tune and now doesn’t the strings are worn out and should be replaced. That means all of them.
2. If the D string doesn’t tune and play correctly but the other strings do, the intonation setting is off. There are plenty of YouTube videos on setting your intonation. You can start by checking the D string only, and if that fixes it, great. If not, set the intonation on all strings. This is best done with new strings that have been stretched/played in a bit so that your result isn’t tainted by a dead or near-dead string.
Good luck in finding your issue and getting it fixed. A guitar that won’t play in tune isn’t much use!
EDIT: Noting veinbuster’s post, I may have omitted a critical first step. You did tune the guitar, right?