In the gospels, why does Jesus sometimes tell the people not to tell anyone after he has performed a miracle?
There are multiple examples of this throughout the gospels.
There are a variety of reasons why each gospel author may have chosen to implement what has come to be called the "messianic secret" (Messiasgeheimnis) theme (based on Wrede's watershed work).1 Most scholarly discussion of this theme is related to the gospel of Mark, which the author of the gospel ascribed to Matthew likely used as a source. Even so, the author of Matthew did not use the theme in the same way as that of Mark, having different emphases and likely a different target audience for his gospel.
According to Wrede, the author of Mark did not intend to give a historical account but rather a (post-resurrection) theological interpretation (this was a controversial idea in 1901 when Wrede first wrote his book in German). Wrede concludes this argument by stating:
[D]uring his earthly life Jesus' messiahship is absolutely a secret and is supposed to be such; no one apart from the confidants of Jesus is supposed to learn about it; with the resurrection, however, its disclosure ensues. This is in fact the crucial idea, the underlying point of Mark’s entire approach.2
While many scholars today don't agree with all of Wrede's propositions,3 the idea that the synoptic gospels are primarily theological rather than historical accounts (at least by modern historical standards) is still widely held. For the author of Mark, the unveiling of Jesus as the Messiah is a central theological motif of the gospel.4
For the gospel of Matthew, the emphasis has to do with the fulfillment of prophecy, which the author explicitly links to this theme in 12:16-21 (NRSV):
[A]nd he ordered them not to make him known. This was to fulfill what had been spoken through the prophet Isaiah:
"Here is my servant, whom I have chosen, my beloved, with whom my soul is well pleased. I will put my Spirit upon him, and he will proclaim justice to the Gentiles. He will not wrangle or cry aloud, nor will anyone hear his voice in the streets. He will not break a bruised reed or quench a smoldering wick until he brings justice to victory. And in his name the Gentiles will hope".
In summary, the answer to this question will differ depending on which gospel is being asked about, as each author has a unique theme and intent when writing their respective works. For the author of Matthew, the use of the "messianic secret" theme is linked to its fulfillment of prophecy (Isaiah 42:1-4).
1 William Wrede, The Messianic Secret: Das Messiasgeheimnis in den Evangelien, trans. J.C.G. Grieg (Cambridge, UK: James Clarke & Co., 1971).
2 Ibid., 68.
3 Cf. Schweitzer, The Quest for the Historical Jesus, 339; Aune, "The Problem of the Messianic Secret," 5; Stein, Mark, 24.
4 The gospel of Luke doesn't give a clear reason for using this theme so I won't elaborate on all of the theories since this question is specifically about a text in Matthew.
As noted by the OP, there are a number of these passage in the Gospel accounts. Collectively, this phenomenon is know as the "Messianic Secret" in academic literature. A number of explanations have been offered for the secrecy passages ranging from Jesus actually said such things for some reason (to teach the 12, to delay his death, to avoid Jewish misconceptions of the Messiah, etc.) to it's a literary device of some sort (let readers know salvation is a secret reserved for the elect) to it's an artifact of early church history (the passages were added to explain why few recognized Jesus as Messiah during his life.) For a thorough list of the options, see my post What explanations have been offered for the “messianic secret” passages? on C.SE.
Summarizing the validity of the various options, it seems clear that historical-critical options such as that the passage arose to explain people not recognizing Jesus as Messiah during his lifetime are pretty unlikely. The reason is that in many cases the command is immediately followed by an indication that it was not followed. For example, Mark 1:43-44:
And Jesus sternly charged him and sent him away at once, and said to him, “See that you say nothing to anyone, but go, show yourself to the priest and offer for your cleansing what Moses commanded, for a proof to them.” (ESV)
is immediately followed by:
But he went out and began to talk freely about it, and to spread the news
which completely defeats the alleged purpose of explaining people's failure to recognize Jesus for who He was.
Distinguishing between literary devices and actual history is harder. In principle, most any aim detected could originate from an "acted parable" technique of Jesus or be a literary creation of the Gospel writers. However, if Mark (under a Markan priority assumption; otherwise whomever it was that wrote first) is employing a literary device, he certainly didn't make it obvious. Hence my the meaning of the passages has been so heavily debated in scholarship. That tends to suggest actual history is the more likely explanation.
In any case, Mark seems to give a partial explanation for the secrecy passages in 8:27-33. In this passage Jesus first asks the 12 what people are saying about him, to which they offer various ideas - John the Baptist, a prophet, etc. He then asks who they say He is and Peter replies "You are the Christ." Jesus tells them tell no one about this, and then starts teaching that he must suffer and die. Peter rebukes Jesus, to which Jesus replies:
Get behind me, Satan! For you are not setting your mind on the things of God, but on the things of man. (Mark 8:33)
This could play into several theories - the expectation of man (earthly leader) is not a "thing of God"; the disciples learned his destiny so they could later comprehend what had happened (i.e. part of a teaching scheme); when the time was right, Jesus would reveal his Messiahship and die, but that time hadn't yet arrived. For example, the Tyndale Commentary on this passage remarks:3
His destiny to die was revealed to them that they might understand the significance. The unveiling of the secret of Jesus' identity as the Messiah paved the way for the unveiling of the mystery of his destiny to die on the cross. The death of Jesus, which Peter so adamantly rejected, the church was called on the embrace.
It seems that the "creation" of secrecy passages by the Gospel writers would be more likely to cause problems for the church than to help it - a guy who is always telling people not to reveal his Messianic identity allows critics to say "even Jesus didn't claim to be the Messiah." Thus, by the criterion of embarrassment, I think the words most likely originate with Jesus himself. As to why Jesus said such things, "to delay His death", "to teach to disciples", and "to redefine the Messiah" all make sense, but none provide a completely satisfactory explanation. Perhaps the best explanation is all three reasons played a role.
The answer to your question can be found in other passages Scripture: Jesus Christ was not above Scripture, thus he could not cross the bounds of what was preordained to occur, set forth, by His Father (God). More specifically, He came to fulfill certain prophecy. Everything that occured had a set time to happen and could not happen an instance before that set time.
Verses to consider to build context upon
» [Mark 9: 9]:
9 - As they were coming down the mountain, Jesus gave them orders not to tell anyone what they had seen until the Son of Man had risen from the dead.
» [John 8: 59]:
59 - Therefore they picked up stones to throw at Him, but Jesus hid Himself and went out of the temple.
» [John 12: 36]:
36 - Believe in the light while you have the light, so that you may become children of light." When he had finished speaking, Jesus left and hid himself from them.
☼[Matthew 26: 51-56]
51 - And behold, one of those who were with Jesus reached and drew out his sword, and struck the slave of the high priest and cut off his ear.
52 - Then Jesus said to him, “Put your sword back into its place; for all those who take up the sword shall perish by the sword.
53 - “Or do you think that I cannot appeal to My Father, and He will at once put at My disposal more than twelve legions of angels?
54 - “How then will the Scriptures be fulfilled, which say that it must happen this way?”
55 - At that time Jesus said to the crowds, “Have you come out with swords and clubs to arrest Me as you would against a robber? Every day I used to sit in the temple teaching and you did not seize Me.
56 - “But all this has taken place to fulfill the Scriptures of the prophets.” Then all the disciples left Him and fled.
There are plenty of passages of Scripture that would seem peculiar at the very least that Jesus Christ our Lord would have to go through, when we know he commanded all the power and glory of God in bodily form. Yet, and however, we learn that He (Jesus) did not come to condemn the world but rather, to fulfill Scripture that was prophecied much earlier in the history of the world. Matthew 26 51-56 gives us red letters (Jesus speaking) of what He could do, that is the authority he commanded, but that He cannot make null His prophecy. If what was prophecied did not take place, or did not happen as it happened then there would be room for falicy and we could not take God at His very word.
In summary, it was because it needed to happen that way to fullfill Scripture.
Thank you for reading,