I don't understand if the counters are in the same word of the numbers, for example do I write 한개 or 한 개?
“한 개” is correct.
Most of units are 의존 명사 (translates to “dependent/bound noun”). And you space before them.
Hangul orthography §5.2.42
의존 명사는 띄어 쓴다. You space before dependent/bound nouns (의존 명사).
And whether a unit is an 의존 명사 or not, you always space before units. This is “recommended” by the standard.
Hangul orthography §5.2.43
단위를 나타내는 명사는 띄어 쓴다. You space before unit nouns.