I'm trying to understand how to create state diagram of DFA. I found following example. On the first diagram I dont understand why we need fourth state when third state is final and there is no transitive function from fourth state to another state. And one more question about second diagram. Can't this automata take the word aaaa?

"Final state" is a confusing term. It does not mean that the state is actually the last one, or that the automaton stops when it reaches the final state. It means that if the automaton reaches that state at the end of the input, then the automaton will accept the input. The automaton might reach a final state at the end of the input, or it might not. For this reason some people prefer to call final states "accepting states".
The situation is further confused because the answer you are given for the left-hand automaton is wrong. The automaton pictured there accepts strings with exactly two
as, not strings with exactly threeas. I will suppose that the question was misprinted, and should say $$\{w \mid \text{$w$ has exactly $\bf{two}$ $\mathtt{a}$'s}\}.$$The left-hand automaton must accept strings with exactly two
as, and it must also reject strings with fewer than 2as or with more than 2as. After reading any string with zeroas, the automaon will be in the leftmost state. After reading any string with exactly onea, it will be in the second state. After reading any string with exactly twoas it will be in the third state, which is an accepting state, so it will accept any such string. But what does it do if the input string contains three or moreas? It must do something; the definition of an automaton says that there must be a transition function which says what the new state is for any previous state and any input symbol. So there must be transitions out of the third state for bothaandb. A thirdatakes the automaton into the fourth state, and once it is in that state it stays there until the input is completely read. Then it rejects the input, because the input had more than twoas.The second automaton does not accept
aaaa, because eachatakes it around the loop leading from the initial state back to the initial state. After readingaaaait finishes in the same state it started in; because this is not an accepting state, the automaton does not accept the stateaaaa.