My teacher says I do it wrong but I dont know when to use the right one
When I do use who and whom?
In formal writing always us "whom" for all objects. In this case 'Whom" requires the objective when it follows a preposition. Formal: Whom did you vote for? or Informal: Who did you vote for? It can be used to begin with a question.
Understanding how to use "who" and "whom" correctly really comes down to being able to discern what the subject of the sentence is and what the object is.
In the following sentences, who is the subject because it is performing the action:
Who lives in that house?
Who drove you to the airport?
In the next sentences, whom is being acted upon, and there is another subject performing the action:
Whom did they talk to? (The subject is they.)
She doesn't know whom to trust. (The subject is she.)
As Mary said, if you can substitute a subject pronoun for who, as in, (He lives in that house), then you should be using who, not whom. But if you can answer your question with an object pronoun, (They talked to her), the correct choice would be whom.
An easy way to determine which to use is to see which pronoun could replace the word.
For example: You are going with him? >>> You are going with WHOM?
WHO is going? >>> He is going.
Thus, if “he” sounds right, use WHO.
If “him” sounds right, use WHOM. Notice: both end in “m”.
The answer is much simpler than you might have thought!
The distinction between using "whom" or "who" in a sentence is a matter of determining whether that third party is the subject or the object of the sentence.
To recap, the subject in a sentence is the acter -- the doer -- the one that is performing an action or doing something. For example, in the sentence "Peter threw the ball," the subject is Peter because he is performing the action of throwing the ball.
The object of the sentence is the thing that is being acted upon -- either being manipulated, moved, addressed, transformed, etc. So in the sentence "Peter threw the ball" the ball is the object of the sentence, because the ball is the thing being acted upon (thrown) in the sentence.
Now onto your question:
In a sentence, use "who" if it describes the subject of the sentence. For example:
"WHO is playing tonight"
"WHO wants dessert?"
"The folks WHO just left the restaurant did not pay their bill!"
Conversely, use "whom" when it refers to the object of the sentence. Note that this includes the object of a preposition ("under", "over", "behind", "between", etc.)!
"To WHOM would you like to speak?" (subject is you, preposition is to, object is whom)
"I can't remember WHOM I went to the bar with last night." (preposition is with, object is whom)
"You were sitting right behind WHOM at the basketball game?!" (preposition is behind, object is whom)
I know the usage of "whom" might sound a little strange, but techinically it's correct.
So remember: In a sentence, the subject ---> "who". The object (including the object of a preposition) ---> "whom".
To add to the Joseph's answer, a little phrase that can be useful for remembering which to use is "Who did what to whom?"