The people most likely to be able to do this are composers and orchestral conductors. It is a mixture of inbuilt talent allied with plenty of practice.
You could argue that composers and conductors become good at this because they practise doing it. In my opinion, it is more likely the other way around. Because they can do it, they become composers and conductors!
Let's look first simply at the ability to hear music in one's head, quite apart from reading and writing it.
I'll speak for myself. I'm not blessed with the ability to do this at will but very often in a state between waking and sleeping, I can hear a full band/orchestra playing. I can pick out different instruments and even get them to play solos.
Here is a thread on the Young Composers website. It is clear from this that abilities to do this vary widely. https://www.youngcomposers.com/t30950/writing-music-in-your-head/ e.g.
Well, I write music in my head ALL the time. The thing is, in my head, the music never stops: See, it can be a piece of someone else's,
a well known piece I'm thinking about, but about 50% of the time it's
a piece of my own. When it is, I always think about some rhythms that
I randomly tap out, then the melody, harmony, etc all come to me at
the same time, all bunched in there. It's so natural for me, I never
even thought about it being unusual. I mean, I also can get the idea
anywhere, so I guess that's just me!
Schumann apparently had exactly the same going on in his head.
The Romantic composer Robert Schumann was said to have heard entire
symphonies in his head from which he drew as inspiration for his
music, but later in his life this phenomenon had diminished to just a
note that played ceaselessly within his head.
Nowadays, this is recognised as a mental (or perhaps rather brain) disorder that can be very distressing for some people.
Musical hallucinations are a form of auditory hallucinations, in which
patients hear songs, instrumental music or tunes, even though no such
music is actually playing. Most patients realize they are
hallucinating, and find the music intrusive and occasionally
unpleasant. There is no cure.
Musical ear syndrome (MES) describes a condition seen in people who
have hearing loss and subsequently develop auditory hallucinations.
"MES" has also been associated with musical hallucinations, which is a
complex form of auditory hallucinations where an individual may
experience music or sounds that are heard without an external
source. It is comparable to Charles Bonnet syndrome (visual
hallucinations in visually impaired people) and some have suggested
this phenomenon could be included under this diagnosis.
Musical hallucinations usually occur in older people. Several
conditions are possible causes or predisposing factors, including
hearing impairment, brain damage, epilepsy, intoxications and
psychiatric disorders such as depression, schizophrenia and
obsessive-compulsive disorder. Hearing impairment is the most common
predisposing condition, but is not by itself sufficient to cause
Mozart had the ability from a young age to hear an entire piece of music at a concert, then go home and write it out as sheet music. He composed this way too. Sometimes he would be lazy and only write out the sheet music the night before a concert.
Here is a thread discussing the topic of hearing scores in one's head just by reading them. https://www.talkclassical.com/67924-those-you-who-can.html
The ability to hear a full orchestral score in one's head from reading the score is what makes a great orchestral conductor. The main job of such a conductor is the get real musicians to play what the conductor already hears in their head!
In response to a comment by @Internet Chord Database, I should make it clear that I don't intend to say that only conductors and composers have this ability. Other musicians do too. Also, for any kinds of musician, abilities will vary from being able hear a single part in your head, all the way up to imagining a whole orchestra.