Not much is known about the mechanisms involved, but they do appear to differ significantly from parasite to parasite. I'll discuss a few different examples here, and try to provide as much information as is currently known about how the parasites are doing it.
There are likely to be many more different examples and mechanisms than these, and researchers may well revise the below hypotheses over time. For this reason, I'm thinking about either splitting this answer into multiple separate answers or changing it into a community wiki.
Example 1 - Toxoplasma gondii and rats as an intermediate host
The Toxoplasma gondii parasite infects multiple hosts during its lifecycle. At one stage, it is hosted by a rodent, which must be eaten by a cat if the parasite is to use the feline's gut in the next stage of its life cycle. Somehow, the parasite causes the rodent (I know that rats are affected, but I don't know if mice or other rodent species are) to behave in a less risk-averse way around cats, making it more likely to be eaten by them.
Exactly how Toxoplasma influences its rodent hosts is unclear. An article in Nature ("Parasite makes mice lose fear of cats permanently") mentions that the parasite creates cysts within the rodent's brain cells, increasing their production of dopamine, which may be one of the causes for the change in behaviour. However, the researchers quoted in the article found that the risk-taking behaviour persisted even without the cysts and dopamine.
Link to that research paper: "Mice Infected with Low-Virulence Strains of Toxoplasma gondii Lose Their Innate Aversion to Cat Urine, Even after Extensive Parasite Clearance".
One theory is that the parasite simultaneously infects the ovaries/testes of the rodent, causing increased testosterone levels in the male and thereby causing it to engage in more risk-taking behaviour. A lot of the literature relevant to this theory is behind paywalls, but as far as I can tell studies have shown that:
Male rats with their testes removed are not affected by the behavioural changes, although I do not know if the orchiectomies were performed before or after infection.
Male rats in general are affected.
Female rats are affected by the behavioural changes, whether or not their ovaries have been removed.
This suggests that the parasite has more than one means of inducing the behavioural changes in the rodent, and that the behavioural changes in female rats have a different cause to the same behavioural changes in males. In the case of the male, it is believed that epigenetics play a part. The increased testosterone levels lead to reduced methylation in genes in a certain area of the brain, and this in turn leads to increased production of the chemical "arginine vasopressin" (AVP). I know nothing about AVP, but the increased levels of it are supposed to cause the behavioural changes, according to this theory.
As I mentioned, the presence of paywalls is giving me some difficulty in studying the relevant research for this particular theory, but a link to one paper is provided in the "Sources" list below.
Another theory is that the parasite infects the white blood cells and causes them to produce excess quantities of a chemical that acts as a neurotransmitter, affecting the brain through the increased levels of said chemical. The research paper is "GABAergic Signaling Is Linked to a Hypermigratory Phenotype in Dendritic Cells Infected by Toxoplasma gondii".
There's also a New Scientist article: "Parasite makes mice fearless by hijacking immune cells")
Example 2 - Ophiocordyceps unilateralis (fungus) and carpenter ants
A recent article in Ars Technica ("Zombifying fungus bypasses the brain to make ants its puppets, study finds") described research into another example, the Ophiocordyceps unilateralis fungus that parasitizes carpenter ants. The researchers had found that the fungal cells in the ant's body effectively disconnected its brain and linked up to act as a replacement brain to move the host's limbs.
Link to the research paper: "Three-dimensional visualization and a deep-learning model reveal complex fungal parasite networks in behaviorally manipulated ants"
(If I understand correctly, the brain's disconnection might be due to damage caused to it by chemicals emitted by the fungus. It may be the case that the original brain and fungal network are both able to move the ant's limbs during the early stages of infection, I'm not sure.)
Even then, one piece at least of the host's altered behaviour wasn't in fact due to this, but due to the fungus destroying a protective membrane on the ants' jaw muscles - leading to the famously strong "death grip" as the infected ant bites a high leaf, dies, and is held in place by the grip of its jaws.
The O. unilateralis fungus extending soma stalks and fruting heads from the bodies of dead ants. Image taken from https://en.wikipedia.org/wiki/File:Ophiocordyceps_unilateralis.png. The image was posted under a Creative Commons Attribution 2.5 Generic license and is credited to David P. Hughes and Maj-Britt Pontoppidan.
The research paper for the jaw membrane is, unfortunately, behind a paywall. If you have access: "Zombie ant death grip due to hypercontracted mandibular muscles"
The "cut off and replace the brain" part of this was also covered by a 2017 article in The Atlantic ("How the Zombie Fungus Takes Over Ants’ Bodies to Control Their Minds").
Example 3 - Dicrocoelium dendriticum and ants
For the actual host/parasite pairing that OP was asking about, not as much is known. One fascinating aspect of this infection is that the ant's behaviour is only changed during the evening/nighttime hours. As day approaches, if the ant has not already been eaten, and if the temperature exceeds a certain threshold, the ant climbs back down to ground level. It rejoins the colony (which does not reject it) and continues to function normally until night falls again, whereupon it climbs back up to the top of the blade of grass.
(In some situations, it may climb up a flower stem and attach itself to a petal.)
A team of researchers used micro-CT scanning to examine the brains of infected ants, publishing their results in a 2018 Nature article ("3D virtual histology at the host/parasite interface: visualisation of the master manipulator, Dicrocoelium dendriticum, in the brain of its ant host")
Most of the parasites are situated in a region of the ant's abdomen known as the "gaster". At this stage in their lifecycle, they are known as "metacercariae". At least one of the parasites, however, will enter a region of the ant's nervous system known as the "suboesophageal ganglion" (SOG). In the brains examined by the researchers, one (and only one) of the parasites in that area would attach its "oral sucker" to the brain tissue of the host ant. This would occur in "the ventral-anteriormost region of the SOG".
The parasites in the gaster are contained within cysts. Some or all of the ones in the brain may not be. The parasite that has attached its oral sucker is not. This one is informally nicknamed "the brain worm".
What is so significant about the SOG region? For one thing:
dendrites of the 10–12 motor neurons that stimulate the fast muscle fibres of the mandibular closure muscles in ants share the same neuropil in the ventral-anteriormost region of the SOG24 – the precise location where non-encysted D. dendriticum metacercariae are located. Thus, by targeting this region of the SOG, D. dendriticum has the potential to directly interfere with ... the action of the mandibles.
Thus we see another mechanism used by a parasite to force its ant host to grip something tightly with its mandibles.
The SOG also has some connection to locomotive activity - presumably getting the ant to climb the blade of grass and/or stopping it from climbing down too soon:
Site-selection and development of non-encysted metacercariae within the SOG can provide a direct pathway for the manipulation of locomotory activity.
And even more...
The SOG is a major part of the nervous system-wide network for neuromodulation in insects, evidenced by the many different neuromodulators expressed in neurons that reside in this region of the brain [25,26,27]. The effects of these neuromodulators ... are diverse and far reaching, from flight modulation, visual dishabituation and olfactory learning to mediation of the fight-or-flight response and the control of aggression ... FMRFamide-like peptide is implicated both as a neurotransmitter and a neuromodulator and affects the nervous system as well as skeletal muscle . Thus, any influence exerted by metacercariae on the activity of neuromodulatory neurons in the SOG could have far reaching consequences for the behaviour of infected ants.
The researchers noted that they did not know if the "brain worm" was in contact with the SOG all the time, or just when the temperature was low enough to induce the changed behaviour. Further research was needed.
Ingram WM, Goodrich LM, Robey EA, Eisen MB (2013). Mice Infected with Low-Virulence Strains of Toxoplasma gondii Lose Their Innate Aversion to Cat Urine, Even after Extensive Parasite Clearance. PLoS ONE 8(9): e75246. https://doi.org/10.1371/journal.pone.0075246
Abdulai-Saiku, S., Vyas, A. Loss of predator aversion in female rats after Toxoplasma gondii infection is not dependent on
ovarian steroids. Brain Behav. Immun. (2017), http://dx.doi.org/10.1016/j.bbi.2017.04.005
Fuks JM, Arrighi RBG, Weidner JM, Kumar Mendu S, Jin Z, Wallin RPA, et al. (2012). GABAergic Signaling Is Linked to a Hypermigratory Phenotype in Dendritic Cells Infected by Toxoplasma gondii. PLoS Pathog 8(12): e1003051. https://doi.org/10.1371/journal.ppat.1003051
Fredericksen MA, Zhang Y, Hazen ML, Loreto RG, Mangold CA, Chen DZ and Hughes DP. (2017). Three-dimensional visualization and a deep-learning model reveal complex fungal parasite networks in behaviorally manipulated ants. Proceedings of the National Academy of Sciences Nov 2017, 114 (47) 12590-12595; DOI: 10.1073/pnas.1711673114
Mangold CA, Ishler MJ, Loreto RG, Hazen ML and Hughes DP. (2019). Zombie ant death grip due to hypercontracted mandibular muscles. Journal of Experimental Biology 2019 222: jeb200683 doi: 10.1242/jeb.200683
Martín-Vega D, Garbout A, Ahmed F. et al. 3D virtual histology at the host/parasite interface: visualisation of the master manipulator, Dicrocoelium dendriticum, in the brain of its ant host. Sci Rep 8, 8587 (2018) doi:10.1038/s41598-018-26977-2