Your question was posed and solved on pp. 468–469 of the classical paper by Erdős and Szekeres, A combinatorial problem in geometry, Compositio Math. 2 (1935), 463–470 (pdf); their argument is paraphrased below. (For a generalization to non-monotonic sequences by Sergey Norin see this Math Overflow question.)
Call a sequence $x_1,\dots,x_n$ convex if $x_2-x_1\le\cdots\le x_n-x_{n-1}$, concave if $x_2-x_1\ge\cdots\ge x_n-x_{n-1}$. For integers $r,s\ge0$ let $F(r,s)$ denote the maximum length of an increasing sequence of real numbers with no convex subsequence of length $r+2$ and no concave subsequence of length $s+2$. Thus $k(n)$ is the greatest integer $r$ such that $F(r-2,r-2)\lt n$.
Theorem. (Erdős and Szekeres) $F(r,s)=\binom{r+s}r$ for $r,s\ge0$. Hence $k(n)$ is the greatest integer $r$ with $\binom{2r-4}{r-2}\lt n$.
Proof. The boundary conditions $F(0,s)=F(r,0)=1$ are obvious; we have to show that $F$ obeys the Pascal recurrence $F(r,s)=F(r-1,s)+F(r,s-1)$ for $r,s\ge1$.
Lemma 1. $F(r,s)\le F(r-1,s)+F(r,s-1)$ for $r,s\ge1$.
Proof. Let $r,s\ge1$, let $n=F(r,s)\ge2$, and let $x_1,\dots,x_n$ be an increasing sequence of length $n$ with no convex subsequence of length $r+2$ and no concave subsequence of length $s+2$. Choose $k\in\{1,\dots,n-1\}$ so that $x_k\le\frac{x_1+x_n}2\le x_{k+1}$. Since $x_k-x_1\le x_n-x_k$, the sequence $x_1,\dots,x_k$ has no convex subsequence of length $r+1$ and no concave subsequence of length $s+2$, so $k\le F(r-1,s)$. Likewise, since $x_n-x_{k+1}\le x_{k+1}-x_1$, the sequence $x_{k+1},\dots,x_n$ has no convex subsequence of length $r+2$ and no concave subsequence of length $s+1$, so $n-k\le F(r,s-1)$. Hence $F(r,s)=n=k+(n-k)\le F(r-1,s)+F(r,s-1)$.
Lemma 2. $F(r,s)\ge F(r-1,s)+F(r,s-1)$ for $r,s\ge1$.
Proof. Let $x_1,\dots,x_m$ be an increasing sequence of length $m=F(r-1,s)$ with no convex subsequence of length $r+1$ and no concave subsequence of length $s+2$. Let $y_1,\dots,y_n$ be an increasing sequence of length $n=F(r,s-1)$ with no convex subsequence of length $r+2$ and no concave subsequence of length $s+1$. If $y_1-x_m\gt\max(x_m-x_1,y_n-y_1)$ then the sequence $x_1,\dots,x_m,y_1,\dots,y_n$ will have no convex subsequence of length $r+2$ and no concave subsequence of length $s+2$, showing that $F(r,s)\ge m+n=F(r-1,s)+F(r,s-1)$.
Remark. Here is an alternative proof of the fact that $k(7)\ge4$. We show that a sequence $x_1,\dots,x_7$ (not necessarily monotonic) has a convex or concave subsequence of length $4$. Consider the graph with vertex set
$$V=\{(r,s,t)\in\mathbb N^3:1\le r\lt s\lt t\le7\}$$
and edge set
$$E=\{\{(r,s,t),(s,t,u)\}\in\binom V2:1\le r\lt s\lt t\lt u\le7\}.$$
Color a vertex $(r,s,t)$ blue if $x_s-x_r\le x_t-x_s$, red if $x_s-x_r\gt x_t-x_s$. Since the graph has odd cycles such as the $7$-cycle
$$(1,2,3),(2,3,4),(3,4,5),(4,5,6),(5,6,7),(3,5,6),(2,3,5),(1,2,3),$$
there must be two adjacent vertices $(r,s,t)$ and $(s,t,u)$ of the same color. Then the sequence $x_r,x_s,x_t,x_u$ is either convex or concave.