The word "se" can have different functions in a sentence, depending on the context. In many cases, such as the one you're asking about, it is related to the verb. In this cases, "se" is substituting a third person. You can find the pronouns "me", "te", "nos" and "vos" replacing 1st person singular, 2nd person singular, 1st person plural and 2nd person plural, respectively. These pronouns work just like "se".
As a general rule, we attach these pronouns to the end of the verb when the verb is in infinitive, gerund or imperative form. In the rest of the tenses, the pronouns are written separately and placed right before the verb. Some examples:
-Me ducho cada mañana.
-¿Te duchas todos los días?
-Javier se ducha antes de dormir.
-¡Dúchate! (imperative)
In the case of verbal periphrasis (two verbs working as one; the first verb being auxiliary, conjugated in a personal tense, and the second verb conjugated in infinitive or gerund form) you can attach the pronoun to the auxiliary verb or to the second verb. The rules are the same as above. In the following examples, both forms are correct and used as often:
-Me tengo que duchar / Tengo que ducharme
-Me voy a lavar los dientes / Voy a lavarme los dientes
-Se puede decir de las dos formas / Puede decirse de las dos formas
Hope this helps!