One may count the entire night (Shulchan Aruch 489:1). The mitzvah begins at night since it is the first opportunity to count [in the Hebrew calendar, each new day begins at night time]. The reason why the best time to count is at night [and according to some, one may only count at night], and not the next morning is since the Posuk says "You should count sheva shabbasos temimos (seven complete weeks)", the earlier one counts in the day makes it more of a complete day (See Mishneh Berurah 489:2 & 4). If one counted before night [shkiya (sunset)], it is too early (Be'ur Halachah 489:3 s.v. me'bod yom) and is as if he did not count that day. Therefore, he must recount with a brachah later. This is very relevant if one accepts Shabbos early, it is too early to count.
If one counted after shkiyah but before tzaitz hakochavim (nightfall), since it is safek lailah (possibly night) he fulfills his obligation (Mishneh Berurah 489:14). Nevertheless, it is preferable to recount without a brachah after taitz hakochavim (Mishneh Berurah 489:15). Someone who normally is stringent to wait for the later tzaitz hakochavim of Rabbeinu Tam to end Shabbos, should preferably wait for that time to count sefiras ha'omer (Koveitz Halachos 2:4).
One who forgot to count during the night, may count the next day until shkiyah, without a brachah (Shulchan Aruch 489:7). The next night he may continue with counting with the brachah (Mishneh Berurah 489:34).
Beginning half an hour before tzaitz hakochavim if one did not yet count, it is prohibited to begin to eat a seudah [i.e. wash on bread or eat more than a k'beitzah of pas haba b'kisnin] or to be involved in any melachah (work) that may cause one to forget to count (Rama 489:4 & Mishneh Berurah 489:23-24). Similarly, one may not go to sleep (Koveitz Halachos 3:1). It is important to note, that these restrictions only apply beginning half an hour before tzaitz hakochavim but not before shkiyah, even if one plans on counting earlier, since tzaitz hakochavim is the ideal time to begin counting (Koveitz Halachos 3:ftnt. 7). If one appoints a shomer (guardian), he may partake in the above mentioned activities. However, only a human being qualifies to be a shomer as opposed to an alarm clock (Koveitz Halachos 3:3). A person who normally davens with a minyan after tzaitz hakochavim, is not required to refrain from the above, since he will count later in shul (Koveitz Halachos 3:4 & ftnt. 10). If someone accepts Shabbos early, if one started eating the Shabbos seudah before half an hour before tzaitz hakochavim one can finish the meal and then count. However, if it is within half an hour, one may not start the seudah until he counts (Koveitz Halachos 3:5).
One should ideally recite the brachah and count himself (Shulchan Aruch 489:1). If one is unable to [either because he is physically unable or is not halachically supposed to], he should hear the brachah from someone else [with the intention to fulfill the mitzvah] and then count himself (Shaar Hatziyon 489:5).
As with all brachos being recited, it is preferable at the time of reciting the brachah to know which day it is. If one did not, he has nevertheless fulfilled his obligation (Mishneh Berurah 489:29).