If $X$ is disconnected then there exist non-empty open sets $U,V\subset X$ such that $U\cap V=\emptyset$ and $X=U\cup V$. Let $f$ be equal to $0$ on $U$ and $1$ on $V$.
For the other direction suppose you have such a map $f$ (needs to be non-constant i.e. attains both values $0$ and $1$) and let $U$ the preimage of $0$ and $V$ the preimage of $1$. Since $\{0,1\}$ has the discrete topology the singletons are open hence $U,V$ are open sets in $X$. Then $U\cap V=f^{-1}(\{0\}\cap\{1\})=\emptyset$. Morever $X=f^{-1}(\{0,1\})=U\cup V$. Namely, $X$ is disconnected.
Since the definition that OP has is that $X$ is disconnected if-f there exists a closed and open set$ B\neq \emptyset, X$ we can show that this definition is equivalent to $X$ is disconnected if it is the disjoint union of two non-empty sets.
$(\Rightarrow)$ $X=B\cup B^c$ where $B^c$ is the complement of $B$. Then $B,B^c$ are non-empty disjoint open sets whose union is $X$.
$(\Leftarrow)$ Let $U,V$ disjoint open sets such that $X=U\cup V$. Then $V$ is the complement of $U$. Hence $U$ is open and closed and since $U,V$ non-empty we have also that $U$ is not $\emptyset$ or $X$.